Dependencies between files with pytest-dependency?

有些话、适合烂在心里 提交于 2019-11-29 10:10:32

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. 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():
    ...
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!