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

前端 未结 9 2225
梦毁少年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:20

    UPDATE: Please take a look at @hpk42 answer. His answer is less intrusive.

    This is what I was actually looking for:

    from _pytest.runner import runtestprotocol
    import pytest
    from _pytest.mark import MarkInfo
    
    def check_call_report(item, nextitem):
        """
        if test method fails then mark the rest of the test methods as 'skip'
        also if any of the methods is marked as 'pytest.mark.blocker' then
        interrupt further testing
        """
        reports = runtestprotocol(item, nextitem=nextitem)
        for report in reports:
            if report.when == "call":
                if report.outcome == "failed":
                    for test_method in item.parent._collected[item.parent._collected.index(item):]:
                        test_method._request.applymarker(pytest.mark.skipif("True"))
                        if test_method.keywords.has_key('blocker') and isinstance(test_method.keywords.get('blocker'), MarkInfo):
                            item.session.shouldstop = "blocker issue has failed or was marked for skipping"
                break
    
    def pytest_runtest_protocol(item, nextitem):
    # add to the hook
        item.ihook.pytest_runtest_logstart(
            nodeid=item.nodeid, location=item.location,
        )
        check_call_report(item, nextitem)
        return True
    

    Now adding this to conftest.py or as a plugin solves my problem.
    Also it's improved to STOP testing if the blocker test has failed. (meaning that the entire further tests are useless)

提交回复
热议问题