With pytest, I'm setting dependencies using the library pytest-dependency
. I also add markers to those tests. Here is an ECM :
# test_test.py
import pytest
@pytest.mark.dependency()
def test_a():
assert True
@pytest.mark.category
@pytest.mark.dependency(depends=['test_a'])
def test_b():
assert True
with the pytest.ini
file to set the marker :
; pytest.ini
[pytest]
markers =
category: category of tests.
When I try to run the test with the marker, as it is dependant on test_a
which doesn't have the marker category
, it is skipped :
user@pc → [~/Documents/test] $ pytest -vv -k category
============================================== test session starts ===============================================
platform darwin -- Python 3.9.8, pytest-6.2.5, py-1.11.0, pluggy-1.0.0 -- /usr/local/opt/[email protected]/bin/python3.9
cachedir: .pytest_cache
rootdir: /Users/saigre/Documents/test, configfile: pytest.ini
plugins: dependency-0.5.1
collected 2 items / 1 deselected / 1 selected
test_test.py::test_b SKIPPED (test_b depends on test_a) [100%]
======================================== 1 skipped, 1 deselected in 0.05s ========================================
Is there a way to force the run of test_a
because of the dependency.
A solution would be to add the marker to the first test, but it would be complicated for the case I'm working on...
EDIT for @MrBean Bremen : I made an example of the scheme of dependency
If I want to add a marker on a test, I would have to put this marker on all the branches, and the "root" would have many markers. It is not that it is complicated to do, but tedious.
You can try this (something similar I have implemented):
Put all your test from the same category in a class, which inherits from a base class that has
test_a
. Put each class in a separate file, and pass that specific file topytest
. This way, you can get rid of markers as well.Consider a pseudo example:
Now, on CLI, just do this:
$ pytest tests/test_b.py