How to skip a test if another test fails with py.test?

Let's say I have these test functions:

def test_function_one(): assert # etc... def test_function_two(): # should only run if test_function_one passes assert # etc. 

How can I make sure test_function_two only works if test_function_one passes (I hope this is possible)?

Edit: I need this because test two uses a property that checks validation.

+7
source share
3 answers

I think the solution for yout would be to mock the value that test1 sets.

Ideally, the tests should be independent, so try to make fun of this value so you can always run test2 whenever you want, and you should also simulate (mock) dirty values ​​so you can see how test2 works if it receives unexpected data .

+1
source

You can use the pytest plugin called pytest-dependency .

The code might look like this:

 import pytest import pytest_dependency @pytest.mark.dependency() #First test have to have mark too def test_function_one(): assert 0, "Deliberate fail" @pytest.mark.dependency(depends=["test_function_one"]) def test_function_two(): pass #but will be skipped because first function failed 
+1
source

I think this is what you want:

 def test_function(): assert # etc... assert # etc... 

This meets your requirement that the second β€œtest” (statement) is performed only if the first β€œtest” (statement) passes.

0
source

All Articles