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.
You can mark a test with the skip and skipif decorators when you want to skip a test in pytest .
PyTest offers a command-line option called maxfail which is used to stop test suite after n test failures.
pytest has the option -x or --exitfirst which stops the execution of the tests instanly on first error or failed test. pytest also has the option --maxfail=num in which num indicates the number of errors or failures required to stop the execution of the tests.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With