简体   繁体   中英

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.

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. 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 ?

The closest similar question is: How to organize fixtures when using pytest

Thanks!

The way I handle this situation is move all fixtures which are shared to a toplevel conftest file like this:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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