我想跳过pytest中针对特定条件的测试但是想要在测试函数内的所有其他条件下执行它。例如,我的测试是验证多个条件,但对于其中一个我想跳过。如果我正在使用pytest.skip,它也会跳过所有其他测试。我不想跳过整个测试,但我的测试中的特殊情况。
例如在下面的代码中,我想跳过if参数有再见,但我想断言所有其他条件,如'hello'和'hi'以及其他断言。说断断续x,y和z
parameter = ['hello','bye',hi]
#i don't want to parameterize it as i am using very long list and
using it in multiple test in different packages
def test function():
#my test have multiple asserts
assert x is true ,'x is not true'
assert y is false, 'y is not false'
for i in parameter:
assert is_present_in_json_file(i)
assert z is true, z is not true'
您可以检查条件并跳过测试(如果匹配)
def test function():
assert x is true ,'x is not true'
assert y is false, 'y is not false'
for i in parameter:
if i == 'bye':
print(f"Skipping this parameter - {i} for reason")
continue
assert is_present_in_json_file(i)
assert z is true, z is not true'