Reputation: 898
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 that test_function_two only runs if test_function_one passes (I'm hoping that it's possible)?
Edit: I need this because test two is using the property that test one verifies.
Upvotes: 19
Views: 10029
Reputation: 1
When you have class defined make sure you use classname along with the testcase name else it will not work
class Test_class():
@pytest.mark.dependency()
def test1(self):
assert False
@pytest.mark.dependency(depends=["classname::test1"])
def test2(self):
print("lastname")
Output: failed=1, skipped=1
Upvotes: 0
Reputation: 458
You can use plugin for pytest called pytest-dependency.
The code can look like this:
import pytest
@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
Upvotes: 12
Reputation: 5971
I'm using the plugin for pytest called pytest-dependency.
Adding to the stated above - if you are using tests inside test classes - you got to add the test class name to the function test name.
For example:
import pytest
class TestFoo:
@pytest.mark.dependency()
def test_A(self):
assert False
@pytest.mark.dependency(depends=['TestFoo::test_A'])
def test_B(self):
assert True
So if test_A fails - test_B won't run. and if test_A passes - test_B will run.
Upvotes: 1
Reputation: 13833
I think that a solution for yout would be to mock the value that the test1 is setting.
Ideally tests should be independent, so try to mock that value so you can always run test2 whenever you want, in fact, you should also simulate (mock) dirty values so you can see how test2 behaves if it receives unexpected data.
Upvotes: 1
Reputation: 3848
I think this is what you want:
def test_function():
assert # etc...
assert # etc...
This meets your requirement that the second "test" (assertion) runs only if the first "test" (assertion) passes.
Upvotes: -1