0

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

5
  • 1
    Then they are not independent tests, and should not be written as such? Commented Feb 7, 2023 at 18:46
  • 1
    You shouldn't assume that test_abc runs before test_xyz in the first place. Commented Feb 7, 2023 at 18:49
  • @Mike'Pomax'Kamermans - so, the question seens exaclty about how to write tests that are not independent - what you pointed out is not a flaw in the question, is a clarification. It is not a common scenario, but I think it is valid. Commented Feb 7, 2023 at 19:42
  • It's also a hint to not write two (or more) test functions, but one test function that does all the dependent checks in sequence. Which is an incredibly common scenario =) Commented Feb 7, 2023 at 20:06
  • 1
    in my scenario the condition is if one test case failed like lets suppose we get element not interactable then it should not run the other test case .but one last test case i want to run anyhow as that test case consist of summary result Commented Feb 8, 2023 at 15:22

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.