pytest how not to exit the for loop after first item assert fails

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,)

2 answers

  • answered 2021-11-29 04:55 badger code

    Using the "try" and "except" keywords in python allows for the errors to not interfere with the program. Was this what you were looking for? You can also change the "out" variable that I used into a list variable if that's what you want. I hope that this helps :)

    James:int=1
    smith:int=2
    Granpa:int=3
    Linda:int=4
    ron:int=5
    
    def test_compare_database()->str:
        out:str=''
        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):
            try:
                assert a==b
                out+="success between "+str(a)+" and "+str(b)+'!'
            except AssertionError as ae:
                out+="there was an assertion error in the program between "+str(a)+" and "+str(b)+'!'
            out+='\n'
    
        return out
    
    print(test_compare_database())
    

  • answered 2021-11-29 05:31 Guy

    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%]
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum