简体   繁体   English

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

[英]Defining a single conftest.py for tests in subdirectories

I have an application where my unit tests are organised like so: 我有一个应用程序,其中单元测试的组织方式如下:

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

In this setup, there's no duplication of fixture config since the conftest.py files have fixtures for only their relevant tests. 在此设置中,夹具配置没有重复,因为conftest.py文件仅具有用于其相关测试的夹具。

Now I'm adding integration tests, organised like so: 现在,我将添加集成测试,其组织方式如下:

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

This new conftest.py needs to have all the fixtures I've defined elsewhere. 这个新的conftest.py需要具有我在其他地方定义的所有固定装置。 How can I setup py.test such that I avoid duplicating all the fixtures from both client/tests/conftest.py and server/tests/conftest.py ? 如何设置py.test,以免避免从client/tests/conftest.pyserver/tests/conftest.py复制所有灯具?

The closest similar question is: How to organize fixtures when using pytest 最接近的类似问题是: 使用pytest时如何组织装置

Thanks! 谢谢!

The way I handle this situation is move all fixtures which are shared to a toplevel conftest file like this: 我处理这种情况的方式是将所有共享的灯具移到顶级conftest文件中,如下所示:

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

This does make it sometimes a bit less nice as you end up with a bunch of not very related fixtures in the toplevel conftest.py, but it is easy and obvious. 确实确实使它有时不太好用,因为您最终在顶层conftest.py中得到了一堆不太相关的固定装置,但是它很容易且显而易见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM