Shastransu Suprabh
Shastransu Suprabh

Reputation: 55

Skip test case if above test case failed pytest

Want to skip some test case below of a test case if test case failed.

class TestExample:
      def test_abc(self):
            print("Hello")
       def test_xyz(self):
             print("Hi")
       def test_aaa(self):
             print("World")
       def test_sample(self):
             print("Python")

Let's suppose test_abc failed , in that scenario want to skip test_xyz and test_aaa and go to test_sample directly

Upvotes: 0

Views: 169

Answers (1)

Override12
Override12

Reputation: 161

You can do that using pytest-dependency plugin. Take the below code as an example

import random
import pytest


class TestExample:
    @pytest.mark.dependency()
    def test_abc(self):
        print("Hello")
        assert random.randint(0, 1)

    @pytest.mark.dependency(depends=["TestExample::test_abc"])
    def test_xyz(self):
        print("Hi")
        assert random.randint(0, 1)

    @pytest.mark.dependency(depends=["TestExample::test_abc", "TestExample::test_xyz"])
    def test_aaa(self):
        print("World")

    def test_sample(self):
        print("Python")

In the example test_xyz will execute only if test_abc is successful, and test_aaa will execute only if both test_abc and test_xyz have passed. test_sample will be always executed as it doesn't depend on anything.

More information about the plugin can be found here: https://pytest-dependency.readthedocs.io/en/stable/usage.html

Upvotes: 1

Related Questions