How to Skip Remaining Tests in a Class If One Fails

How to Skip Remaining Tests in a Class If One Fails

21 May 2024 Stephan Petzl Leave a comment Tech-Help

When creating test cases for web applications using frameworks like Jenkins, Python, Selenium, and Pytest, it’s common to organize tests in a class structure where each class represents a test case and each method represents a step in that case. This setup works well when all tests pass, but can become problematic when one step fails, causing subsequent steps to fail unnecessarily.

Problem Statement

The goal is to skip the remaining test methods in a class if one of them fails. This prevents false positives and ensures that only relevant failures are reported. The solution should avoid manual checks in each test method and leverage Pytest’s hooks for a cleaner implementation.

Solution

Here’s a solution that uses Pytest’s official hook extensions, which can be added to your conftest.py file:

import pytest

def pytest_runtest_makereport(item, call):
    if "incremental" in item.keywords:
        if call.excinfo is not None:
            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)
    

With this implementation, you can define a test class with the @pytest.mark.incremental decorator:

import pytest

@pytest.mark.incremental
class TestUserHandling:
    def test_login(self):
        pass
    def test_modification(self):
        assert 0
    def test_deletion(self):
        pass
    

Running the tests with the -rx option will report on the reasons for skipped tests:

pytest -rx

Alternative Approaches

For those who prefer a different approach, the pytest --maxfail=N option can be used to stop test execution after a specified number of failures:

pytest --maxfail=1

This method is useful if you want to stop all tests after a certain number of failures, regardless of their class or structure.

Advanced Use Cases

If you need even more control, such as stopping tests based on specific criteria, you might consider using the pytest-dependency plugin, which allows you to skip tests if certain other tests have failed:

pip install pytest-dependency

This can be particularly useful in complex test environments where dependencies between tests need to be managed explicitly.

Enhancing Test Automation with Repeato

For those looking to streamline their test automation efforts, Repeato offers a no-code solution for creating, running, and maintaining automated tests for iOS and Android applications. Repeato’s intuitive test recorder and computer vision-based approach make it fast and easy to edit and run tests. Additionally, Repeato supports testing websites inside Android emulators or devices, with explicit web testing support coming later this summer.

For more information on how Repeato can enhance your test automation workflow, visit our documentation or download page.

Like this article? there’s more where that came from!