Dependencies between files with pytest-dependency?

9k views Asked by At

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.

1

There are 1 answers

2
hoefling On BEST ANSWER

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:

# conftest.py
from pytest_dependency import DependencyManager

DependencyManager.ScopeCls['module'] = DependencyManager.ScopeCls['session']

Now test_two from your example will resolve the dependency to test_one. However, this is just a dirty hack for demonstration purposes that will easily corrupt the dependencies once you add another test named test_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 maintainer It is now accepted.


You can use that instead:
$ pip uninstall -y pytest-dependency
$ pip install git+https://github.com/JoeSc/pytest-dependency.git@master

Now the dependency mark accepts an additional arg scope:

@pytest.mark.dependency(scope='session')
def test_one():
    ...

You will need to use the full test name (as printed by pytest -v) in order to depend on test_one in another module:

@pytest.mark.dependency(depends=['test_one.py::test_one'], scope='session')
def test_two():
    ...

Named dependencies are also supported:

@pytest.mark.dependency(name='spam', scope='session')
def test_one():
    ...

@pytest.mark.dependency(depends=['spam'], scope='session')
def test_two():
    ...