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.