[TIP] Can output of failed py.test instances be configured?

Thijs Engels thijs at buckazoids.com
Fri Aug 10 00:32:51 PDT 2012


Dear all,

Due to some legacy setups I am facing a series of tests using py.test,
but with multiple (read: many) assert statements within a single test
function. All of this works as expected, but I was wondering whether I
could configure the output for failing test cases. By default py.test
(rightfully) shows the whole test function up until the failing assert
statement. Is it possible to merely show the failing assert statement?

To clarify my question; the my alteration to the example in py.test
documentation
(http://pytest.org/latest/getting-started.html#our-first-test-run)

# content of test_sample.py
def func(x):
    return x + 1

def test_answer():
    assert func(1) == 2
    assert func(2) == 3
    assert func(3) == 4
    assert func(4) == 5
    assert func(5) == 6
    assert func(6) == 99
    assert func(7) == 8
    assert func(8) == 9

Running this produces this output:

[...]
    def test_answer():
        assert func(1) == 2
        assert func(2) == 3
        assert func(3) == 4
        assert func(4) == 5
        assert func(5) == 6
>       assert func(6) == 99
E       assert 7 == 99
E        +  where 7 = func(6)
[...]

Although I fully realize that multiple assert statements (I am talking
close to 100) is bad practice, I wonder whether I *could* configure the
output.

[...]
    def test_answer():
>       assert func(6) == 99
E       assert 7 == 99
E        +  where 7 = func(6)
[...]

Thanks in advance,

Thijs



More information about the testing-in-python mailing list