I am new to python and pytest. I am trying to assert 2 lists of data. As the first data in the list is not matched, the assertion fails at that point and do not proceed for the next item. However I don't want my execution to stop , but complete the loop and capture the overall assertion results. Can someone help me how to achieve it?
code
def test_compare_database():
records_src = [(1,James,smith,123),(2,Granpa,smith,124),(3,Linda,smith,123)]
records_trg = [(1,James,smith,111),(2,Granpa,ron,124),(3,Linda,smith,123)]
for a, b in zip(records_src, records_trg):
assert a == b
Output:The first item in the list comparison is failed which is correct. But it stops at that. I would want the whole for loop to run and capture the failed results.
============================= test session starts =============================
collecting ... collected 1 item
main_test.py::test_compare_database FAILED
def test_compare_database():
records_src = [(1,James,smith,123),(2,Granpa,smith,124),(3,Linda,smith,123)]
records_trg = [(1,James,smith,111),(2,Granpa,ron,124),(3,Linda,smith,123)]
for a, b in zip(records_src, records_trg):
> assert a == b
E AssertionError: assert (1,
'James',
'Smith',
123,)
!= (1,
'James',
'Smith',
111,)
You can use parametrized test to create multiple tests based on the data, so each pair of tuples will be asserted separately in a different test
def data_source():
records_src = [(1, 'James', 'smith', 123), (2, 'Granpa', 'smith', 124), (3, 'Linda', 'smith', 123)]
records_trg = [(1, 'James', 'smith', 111), (2, 'Granpa', 'ron', 124), (3, 'Linda', 'smith', 123)]
for a, b in zip(records_src, records_trg):
yield a, b
@pytest.mark.parametrize('a, b', data_source())
def test_compare_database(a, b):
assert a == b
Console output:
main_test.py::test_compare_database[a0-b0] FAILED [ 33%]
Tests\main_test.py:33 (test_compare_database[a0-b0])
(1, 'James', 'smith', 123) != (1, 'James', 'smith', 111)
Expected :(1, 'James', 'smith', 111)
Actual :(1, 'James', 'smith', 123)
<Click to see difference>
a = (1, 'James', 'smith', 123), b = (1, 'James', 'smith', 111)
@pytest.mark.parametrize('a, b', data_source())
def test_compare_database(a, b):
> assert a == b
E AssertionError: assert (1, 'James', 'smith', 123) == (1, 'James', 'smith', 111)
main_test.py:36: AssertionError
FAILED [ 66%]
Tests\main_test.py:33 (test_compare_database[a1-b1])
(2, 'Granpa', 'smith', 124) != (2, 'Granpa', 'ron', 124)
Expected :(2, 'Granpa', 'ron', 124)
Actual :(2, 'Granpa', 'smith', 124)
<Click to see difference>
a = (2, 'Granpa', 'smith', 124), b = (2, 'Granpa', 'ron', 124)
@pytest.mark.parametrize('a, b', data_source())
def test_compare_database(a, b):
> assert a == b
E AssertionError: assert (2, 'Granpa', 'smith', 124) == (2, 'Granpa', 'ron', 124)
main_test.py:36: AssertionError
PASSED [100%]