Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 703
Nimit Shah Avatar asked May 29 '26 12:05

Nimit Shah


1 Answers

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.

like image 56
Rohit Avatar answered Jun 01 '26 04:06

Rohit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!