简体   繁体   中英

Why doesn't pytest load conftest.py when running only a subset of tests?

Here is my API test directory layout:

api_tests
├── conftest.py
└── query
    └── me_test.py

Contents of conftest.py:

print("CONFTEST LOADED")

Contents of me_test.py:

"""Tests the "me" query"""

def test_me():
    assert True

If I simply run pytest , everything works:

================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collecting ... CONFTEST LOADED
collected 3 items                                                                                                     

api_tests/query/me_test.py .                                                                                    [ 33%]
lib/myproject/utils_test.py .                                                                                   [ 66%]
lib/myproject/schema/types/scalars_test.py .                                                                    

Notice "CONFTEST LOADED" is printed. Great! However, this test run also picked up all my unit tests, which I don't want. I want to separate my test runs into unit tests and API tests, I don't want to run them all in one go.

However, if I simply run pytest api_tests/ :

================================================= test session starts =================================================
platform linux -- Python 3.8.5, pytest-6.1.0, py-1.9.0, pluggy-0.13.1
rootdir: /home/hubro/myproject, configfile: pytest.ini
collected 1 item                                                                                                      

api_tests/query/me_test.py .                                                                                    [100%]

================================================== 1 passed in 0.00s ==================================================

Now the right tests are run, but the conftest.py file wasn't loaded... How come?


I am using Pytest 6.1.0 on Python 3.8.


EDIT: Alright, I found an acceptable workaround. I can override INI file options through the command line with the -o option. This works:

poetry run pytest -o "testpaths=api_tests"

However, I would very much like an answer to the original question so I'm not going to delete it.

The conftest plugin will be registered in both invocations, the only difference being the registration stage. If in doubt, add the --traceconfig argument to list the registered plugins in order of their registration:

$ pytest --traceconfig
PLUGIN registered: <_pytest.config.PytestPluginManager object at 0x7f23033ff100>
PLUGIN registered: <_pytest.config.Config object at 0x7f2302d184c0>
...
=================================== test session starts ===================================
...
PLUGIN registered: <module 'conftest' from 'path/to/conftest.py'>
...

In the first invocation, the conftest.py won't be found immediately since it's down the test root path, so it will be loaded while pytest discovers the tests. In the second invocation, conftest.py is located in test root, so it will be loaded even before the test session starts (after the plugins passed via -p arg and registered via setuptools entrypoint are loaded). Running pytest -s (with output capturing disabled) should reveal the custom print, located above the ==== test session starts ==== line.

If you want the print to be identical between the two invocations, put it in a suitable hook. For example, to always print CONFTEST loaded after test collection finished, use:

# api_tests/conftest.py

def pytest_collectreport(report):
    print("CONFTEST loaded")

There are other options available for custom output placement; best is to check out the list of available hooks under Hooks in pytest reference.

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