Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: show extra test summary info with -r

Soon we’ll learn about the ability to mark certain test functions as expected to fail and others to be skipped in some condition. When running these tests we’ll want to be able list these based on their special feature.

Using the -v flag would just list all of them which, if you have a lot of tests, would be messy.

The letters one can use with the -r flag:

  • (f)ailed
  • (E)error
  • (s)skipped
  • (x)failed
  • (X)passed
  • (p)passed
  • (P)passed with output
  • (a)all except pP
pytest -rx  - xfail, expected to fail
pytest -rs  - skipped
pytest -ra  - all the special cases
import pytest

def test_pass():
    assert True

def test_fail():
    assert False

@pytest.mark.skip(reason="Unconditional skip")
def test_with_skip():
    assert True

@pytest.mark.skipif(True, reason="Conditional skip")
def test_with_skipif():
    assert True

@pytest.mark.skipif(False, reason="Conditional skip")
def test_with_skipif_but_run():
    assert True


@pytest.mark.xfail(reason = "Expect to fail and failed")
def test_with_xfail_and_fail():
   assert False

@pytest.mark.xfail(reason = "Expect to fail but passed")
def test_with_xfail_but_pass():
   assert True