Search code examples
pythonloopspytestassertallure

Assert in for loop in Python


I have a list of variables which I have to assert to 0. now the code has to be a part of pytest framework & compatible with allure reporting. Since the assert I use declares only 1 test case is there anyway I check every assert as a diff scn?

list = [0,1,0,1,0,0,0]
for x in list:
    assert x == 0

When I run the above code it passes off as 1 test case. Is there any other way rather than specifying each element, to have allure/pytest treat each assert as a diff test case?


Solution

  • Option 1 Here is the solution for you question

    import pytest
    
    list = [0,1,0,1,0,0,0]
    @pytest.mark.nondestructive
    @pytest.mark.parametrize("item",list)
    def test_stackquestion(item):
        assert item == 0
    

    Option 2 if you want to get the result within one test and don't want the test to terminate after each assert and gives collective result of all the asserts you can implement soft assert using delayed assert too.