I recently updated a project from Python 3.10 to 3.11 which broke my ability to run tests through PyCharm. The issue is that fixtures in conftest are no longer recognized, and nearly all of the tests I work with are organized in that way. In fact, it doesn't look like the conftest files are even executed now. I've tried adding break points and print statements within conftest files in several file locations and none of them hit.
The following code works if it's all in one file
@pytest.fixture(scope="session")
def work():
print('work')
return 'work'
def test_with_fixture(work):
print(f'Fixture work -- {work}')
However if I move the fixture to a conftest.py file the test fails with
test setup failed
file project/path/test_file.py, line 27
def test_with_fixture(work):
E fixture 'work' not found
> available fixtures: _session_faker, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, class_mocker, cov, doctest_namespace, faker, mocker, module_mocker, monkeypatch, no_cover, package_mocker, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, requests_mock, session_mocker, testrun_uid, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory, worker_id
> use 'pytest --fixtures [testpath]' for help on them.
I have confirmed that the run config is using pytest.
I have also double checked the settings and ensured that the Project Structure > Content Root is correct, and that Python Integrated Tools > Testing is set to pytest
.
If I revert back to Python 3.10 the tests once again pass. If I run the tests outside of PyCharm they also pass. So it seems specific to PyCharm + Python 3.11. I've tried this with several versions of PyCharm including the current latest (2023.2) and all have the same issue.
Losing the ability to run tests through PyCharm is a big loss for me and I'm hoping that there is a solution.
Edit: After some further debugging I find few more things of note.
- Putting a conftest file in the exact same directory as the test being run does work, but no other locations do.
- I tried running a test from both a terminal and through pycharm with
--trace-config
, which revealed two differences. The non-PyCharm version registered all of the conftest files, as expected. The PyCharm version also has two lines in the trace that do not appear when run outside of it.
PLUGIN registered: <module 'teamcity.pytest_plugin' from '/Applications/PyCharm 3.app/Contents/plugins/python/helpers/pycharm/teamcity/pytest_plugin.py'>
PLUGIN registered: <class '__main__.Plugin'>
Is it possible that one of those modules are somehow breaking the discovery of conftest files? I'm not yet sure how to confirm that but it seems like a potential clue.