How to skip the rest of tests in the class if one has failed?

前端 未结 9 2284
梦毁少年i
梦毁少年i 2020-11-28 06:45

I\'m creating the test cases for web-tests using Jenkins, Python, Selenium2(webdriver) and Py.test frameworks.

So far I\'m organizing my tests in the following str

9条回答
  •  忘掉有多难
    2020-11-28 07:05

    Based on hpk42's answer, here's my slightly modified incremental mark that makes test cases xfail if the previous test failed (but not if it xfailed or it was skipped). This code has to be added to conftest.py:

    import pytest
    
    try:
        pytest.skip()
    except BaseException as e:
        Skipped = type(e)
    
    try:
        pytest.xfail()
    except BaseException as e:
        XFailed = type(e)
    
    def pytest_runtest_makereport(item, call):
        if "incremental" in item.keywords:
            if call.excinfo is not None:
                if call.excinfo.type in {Skipped, XFailed}:
                    return
    
                parent = item.parent
                parent._previousfailed = item
    
    def pytest_runtest_setup(item):
        previousfailed = getattr(item.parent, "_previousfailed", None)
        if previousfailed is not None:
            pytest.xfail("previous test failed (%s)" % previousfailed.name)
    

    And then a collection of test cases has to be marked with @pytest.mark.incremental:

    import pytest
    
    @pytest.mark.incremental
    class TestWhatever:
        def test_a(self):  # this will pass
            pass
    
        def test_b(self):  # this will be skipped
            pytest.skip()
    
        def test_c(self):  # this will fail
            assert False
    
        def test_d(self):  # this will xfail because test_c failed
            pass
    
        def test_e(self):  # this will xfail because test_c failed
            pass
    

提交回复
热议问题