具有pytest-dependency的文件之间的依赖关系?
我正在使用带有pytest-
dependency的pytest的功能测试套件。我99%的人喜欢这些工具,但是我不知道如何在一个文件中进行测试取决于另一个文件中的测试。理想情况下,我希望对受抚养人的零变更要求,并且只更改受抚养人中的东西。我希望测试能够像这样都依赖于test_one:
# 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
像这样:
# contents of test_two.py
@pytest.mark.dependency(depends=["test_one"])
def test_two():
# do stuff
当我pytest test_one.py
正确运行时,它会排序(test_point_one
如果test_one
失败pytest
test_two.py
则跳过),但是当我运行时,它会跳过test_two
。
我尝试添加import test_one
到test_two.py无济于事,并验证了导入实际上是正确导入的-
不仅仅是通过pytest传递过来,“哦,嘿,我已经完成了测试的收集,我无能为力”跳过!万岁,懒惰!”
我知道我可以在技术上把test_two()
在test_one.py
和它的工作,但我不希望只是倾倒在一个文件中的每个测试(这是什么,这将最终退化为)。我试图通过将所有东西放在正确的架子上来保持整洁,而不仅仅是将它们全部推入壁橱。
另外,我意识到如果可以做到的话,存在创建循环依赖的可能性。我对此很好。如果我像这样用脚开枪,说实话,那是我应得的。
-
当前状态,2018年5月31日,
pytest-dependency==0.3.2
目前,
pytest- dependency
仅在模块级别执行依赖项解析。尽管有一些基本的解决方案可以解决会话范围的依赖关系,但在撰写本文时尚未提供完整的支持。您可以通过滑动会话范围而不是模块范围来进行检查:# conftest.py from pytest_dependency import DependencyManager DependencyManager.ScopeCls['module'] = DependencyManager.ScopeCls['session']
现在
test_two
从您的示例中将依赖项解析为test_one
。但是,这只是出于演示目的的肮脏技巧,一旦添加了另一个名为test_one
so的测试,就很容易破坏依赖关系。解决方案建议
有一个PR在会话和类级别添加了依赖项解析,但是软件包维护者尚未接受它。您可以改用:
$ pip uninstall -y pytest-dependency $ pip install git+https://github.com/JoeSc/pytest-dependency.git@master
现在,
dependency
标记接受了一个额外的argscope
:@pytest.mark.dependency(scope='session') def test_one(): ...
您将需要使用完整的测试名称(由打印
pytest -v
),以便依赖test_one
于另一个模块:@pytest.mark.dependency(depends=['test_one.py::test_one'], scope='session') def test_two(): ...
还支持命名依赖项:
@pytest.mark.dependency(name='spam', scope='session') def test_one(): ... @pytest.mark.dependency(depends=['spam'], scope='session') def test_two(): ...