Pytest not collecting tests in dependency order

Viewed 54

I'm using pytest to test my package and I'm facing problems with dependency of tests, cause pytest is collecting (and testing) the sequence in alphabetical order, while it should be in dependency order. How can I change it?

More details:

The sequence of test it should runis:

  1. materials: Needs nothing
  2. onerodtraction, onerodtorsion, onerodbending: Needs (1)
  3. onerodallcharges: Needs the three above (2)
  4. examples: Needs the (3)

When I run I receive:

tests\test_examples.py ssssssssssss                                                         [ 22%]
tests\test_material.py ...                                                                  [ 27%]
tests\test_onerodallcharges.py sss                                                          [ 33%]
tests\test_onerodbending.py ..................                                              [ 66%]
tests\test_onerodtorsion.py .........                                                       [ 83%]
tests\test_onerodtraction.py .........                                                      [100%]

As we see, every test in the third file is skipped, and then the same happens for the first file.

2 Answers

May be you can try this pytest_order

pip install pytest-order

After installing you can mark on your classes.

https://pytest-dev.github.io/pytest-order/stable/usage.html#markers-on-class-level

In the each python file on each class specify the order as said

@pytest.mark.order(1)
class test_onerodtraction:

@pytest.mark.order(1)
class test_onerodtorsion:

@pytest.mark.order(1)
class test_onerodbending:

and now

@pytest.mark.order(2)
class test_onerodallcharges:

and then

@pytest.mark.order(3)
class test_examples.py:
Related