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: compare numbers

def double(n):
    #return 2*n
    return 2+n

def test_double():
    assert double(2) == 4
    assert double(21) == 42
$ pytest -q test_number_equal.py
F                                                                        [100%]
=================================== FAILURES ===================================
_________________________________ test_double __________________________________

    def test_double():
        assert double(2) == 4
>       assert double(21) == 42
E       assert 23 == 42
E        +  where 23 = double(21)

test_number_equal.py:7: AssertionError
=========================== short test summary info ============================
FAILED test_number_equal.py::test_double - assert 23 == 42
1 failed in 0.09s

In this case it is easy to see what is the difference between the expected and the actual value.

Well except that pytest does not have a notion of which side is the expected side and which is the actual value, but you can clearly see if from the code.