pytest Quick Start Guide
上QQ阅读APP看书,第一时间看更新

Lists

Assertion failures for lists also show only differing items by default:

____________________ test_get_starting_equiment _____________________

def test_get_starting_equiment():
expected = ['long sword', 'shield']
> assert get_starting_equipment('warrior') == expected
E AssertionError: assert ['long sword'...et', 'shield'] == ['long sword', 'shield']
E At index 1 diff: 'warrior set' != 'shield'
E Left contains more items, first extra item: 'shield'
E Use -v to get the full diff

tests\test_assert_demo.py:71: AssertionError

Note that pytest shows which index differs, and also that the -v flag can be used to show the complete difference between the lists:

____________________ test_get_starting_equiment _____________________

def test_get_starting_equiment():
expected = ['long sword', 'shield']
> assert get_starting_equipment('warrior') == expected
E AssertionError: assert ['long sword'...et', 'shield'] == ['long sword', 'shield']
E At index 1 diff: 'warrior set' != 'shield'
E Left contains more items, first extra item: 'shield'
E Full diff:
E - ['long sword', 'warrior set', 'shield']
E ? ---------------
E + ['long sword', 'shield']

tests\test_assert_demo.py:71: AssertionError

If the difference is too big, pytest is smart enough to show only a portion to avoid showing too much output, displaying a message like the following:

E         ...Full output truncated (100 lines hidden), use '-vv' to show