为子目录中的测试定义单个conftest.py

时间:2014-06-08 23:12:32

标签: python pytest

我有一个应用程序,我的单元测试组织如下:

server/
  tests/
    conftest.py
    test_server.py
client/
  tests/
    conftest.py
    test_client.py

在此设置中,由于conftest.py文件只有相关测试的固定装置,因此没有夹具配置的重复。

现在我正在添加集成测试,组织如下:

tests/
  conftest.py
  test_integration.py
server/
  tests/
    conftest.py
    test_server.py
client/
  tests/
    conftest.py
    test_client.py

这个新conftest.py需要全部我在别处定义的灯具。如何设置py.test以避免重复client/tests/conftest.pyserver/tests/conftest.py中的所有灯具?

最接近的类似问题是: How to organize fixtures when using pytest

谢谢!

1 个答案:

答案 0 :(得分:1)

我处理这种情况的方法是将所有共享的灯具移动到这样的顶级conftest文件中:

conftest.py
tests/
    [conftest.py]
    test_integration.py
server/
    tests/
        [conftest.py]
        test_server.py
client/
    tests/
        [conftest.py]
        test_client.py

这确实有点不太好看,因为你最终在toplevel conftest.py中得到了一堆不太相关的灯具,但这很简单明了。