Stanislaw Bobritsky
Stanislaw Bobritsky

Reputation: 165

[py.test]: test dependencies

I'm writing test system using py.test, and looking for a way to make particular tests execution depending on some other test run results.

For example, we have standard test class:

import pytest

class Test_Smoke:
   def test_A(self):
       pass

   def test_B(self):
       pass

   def test_C(self):
       pass

test_C() should be executed if test_A() and test_B() were passed, else - skipped.

I need a way to do something like this on test or test class level (e.g. Test_Perfo executes, if all of Test_Smoke passed), and I'm unable to find a solution using standard methods (like @pytest.mark.skipif).

Is it possible at all with pytest?

Upvotes: 16

Views: 17676

Answers (2)

Roy Shilkrot
Roy Shilkrot

Reputation: 3548

Warning: Last release: Apr 5, 2020, this project is not tested for versions higher than 3.8

Consider also pytest-depends:

def test_build_exists():
    assert os.path.exists(BUILD_PATH)

@pytest.mark.depends(on=['test_build_exists'])
def test_build_version():
    # this will skip if `test_build_exists` fails...

Upvotes: 2

azmeuk
azmeuk

Reputation: 4506

You might want to have a look at pytest-dependency. It is a plugin that allows you to skip some tests if some other test had failed.

import pytest

class Test_Smoke:
   @pytest.mark.dependency()
   def test_A(self):
       pass

   @pytest.mark.dependency()
   def test_B(self):
       pass

   @pytest.mark.dependency(depends=['test_A', 'test_B'])
   def test_C(self):
       pass

Upvotes: 8

Related Questions