I'm working on a functional test suite using pytest with pytest-dependency. I 99% love these tools, but I can't figure out how to have a test in one file depend on a test in another file. Ideally, I'd like to have zero changes required to the dependee, and only change things in the depender. I'd like tests to be able to depend on test_one both like this:
# contents of test_one.py
@pytest.mark.dependency()
def test_one():
# do stuff
@pytest.mark.dependency(depends=["test_one"])
def test_point_one():
# do stuff
And like this:
# contents of test_two.py
@pytest.mark.dependency(depends=["test_one"])
def test_two():
# do stuff
When I run pytest test_one.py
it correctly orders things (and skips test_point_one
if test_one
fails), but when I run pytest test_two.py
, it skips test_two
.
I've tried adding import test_one
to test_two.py to no avail, and verified that the import is actually importing properly - it's not just getting passed over by pytest going "Oh hey, I've finished collecting tests, and there's nothing that I can't skip! Hooray for laziness!"
I know I could technically put test_two()
in test_one.py
and it would work, but I don't want to just dump every test in a single file (which is what this would ultimately devolve into). I'm trying to keep stuff tidy by putting everything on the right shelf, not just shoving it all into the closet.
Also, I realize the possibility of creating circular dependencies would exist if this is something I can do. I'm okay with this. If I shot myself in the foot like that, let's be honest, I'd deserve it.
Current status, 31-May-2018,
pytest-dependency==0.3.2
At the moment,
pytest-dependency
does the dependency resolution on module level only. Although there is some rudimentary implementation for resolving session-scoped dependencies, the full support is not implemented at the moment of writing this. You can check that by slipping session scope instead of module scope:Now
test_two
from your example will resolve the dependency totest_one
. However, this is just a dirty hack for demonstration purposes that will easily corrupt the dependencies once you add another test namedtest_one
so read further.Solution proposal
There is a PR that adds the dependency resolution on session and class levels,
but it's not accepted yet by the package maintainerIt is now accepted.You can use that instead:Now the
dependency
mark accepts an additional argscope
:You will need to use the full test name (as printed by
pytest -v
) in order to depend ontest_one
in another module:Named dependencies are also supported: