pytest.mark.skipif not skiping the test if the condition is True

Created on 1 Oct 2019  路  3Comments  路  Source: pytest-dev/pytest

I added a boolean flag variable in the test suit as "test_flag" and assigned a value as False initially.
If any dependant test is failed, I am changing the flag value as True and in next test case I added check in skipif to check the boolean value and skip the test.

Example:

class demo():
    test_flag = False

    def test_demo1(self):
        assert 0 == 1

    @pytest.mark.skipif(test_flag, reason="Skip the test")
    def test_demo2(self):
        assert 1 == 1

#Fixture definitiion
def pytest_runtest_protocol(item, nextitem):
    reports = runtestprotocol(item, nextitem=nextitem)
    for report in reports:
        if report.outcome.upper() == "FAILED":
            if item.name.split("[")[0] in ("test_create_sid"):
                item.cls.test_flag = True
    return True

There no issues with modifying the flag value. But even though the flag value is True, the test is not getting skipped.

Kindly let me know the fix for this issue.

question

All 3 comments

thats not how python works, the moment you pass that reference along, it no longer matters what you do to the old name

if you want too sort this out, you need to use a different way to switch the flag

Decorators at class-level run at import time, so indeed this isn't going to work. You could instead check the flag inside the test and use pytest.skip imperatively instead - or use a fixture from the test and do the same check inside the fixture.

@The-Compiler thanks for the suggestion, I am sure this approach will work.

Was this page helpful?
0 / 5 - 0 ratings