PyCharm运行Pytest测试时无法识别根conftest.py中Fixtures的问题
PyCharm运行单个Pytest测试时无法识别根目录conftest.py中的Fixtures,但终端执行正常
项目目录结构
product-testing/ ├── conftest.py # 根目录通用Fixtures ├── tests/ │ └── grpc_tests/ │ └── collections/ │ └── test_collections.py └── fixtures/ └── collections/ └── conftest.py # 测试专用Fixtures
问题现象
在PyCharm中点击测试函数旁的「运行按钮」执行单个测试时,Pytest无法初始化根目录conftest.py中的Fixtures,但通过终端执行pytest .或指定单个测试的命令(如pytest -k "test_create_multicollection" ./tests/grpc_tests/collections/)时完全正常。
测试代码示例
import datetime import allure from faker import Faker from fixtures.collections.conftest import collection fake = Faker() @allure.title("获取集合列表") def test_get_collections_list(collection, postgres): with allure.step("发送获取集合的请求"): response = collection.collection_list( limit=1, offset=0, # ...其余代码 )
专用Fixtures文件(fixtures/collections/conftest.py)片段
import datetime import pytest from faker import Faker from path.to.file import pim_collections_pb2 fake = Faker() @pytest.fixture(scope="session") def collection(grpc_pages): def create_collection(collection_id=None, store_name=None, items_ids=None, **kwargs): default_params = { "id": collection_id, "store_name": store_name, "item_ids": items_ids, "is_active": True, "description_eng": fake.text(), # 其余代码
根目录通用Fixtures文件(conftest.py)内容
import pytest from faker import Faker from pages.manager import DBManager, GrpcPages, RestPages fake = Faker() @pytest.fixture(scope="session") def grpc_pages(): return GrpcPages() @pytest.fixture(scope="session") def rest_pages(): return RestPages() @pytest.fixture(scope="session") def postgres(): return DBManager() #其他代码
错误信息
PyCharm运行测试时抛出的错误:
test setup failed file .../tests/grpc_tests/collections/test_collections.py, line 9 @allure.title("创建多集合") def test_create_multicollection(collection, postgres): file .../fixtures/collections/conftest.py, line 10 @pytest.fixture(scope="session") def collection(grpc_pages): E fixture 'grpc_pages' not found > available fixtures: *session*faker, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, collection, doctest_namespace, factoryboy_request, faker, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them.
尝试过的解决方案及问题
尝试直接从根目录conftest.py导入缺失的Fixtures:
from conftest import grpc_pages
单独运行测试时有效,但执行pytest .等全局命令时出现导入错误:
ImportError while importing test module '/path/to/test_collections.py'. Hint: make sure your test modules/packages have valid Python names. Traceback: .../importlib/init.py:127: in import_module return bootstrap.gcd_import(name[level:], package, level) tests/.../test_collections.py:6: in <module> from conftest import grpc_pages E ImportError: cannot import name 'grpc_pages' from 'conftest' (/path/to/fixtures/users/conftest.py)
Pytest会优先从fixtures/users/conftest.py查找导入,而非根目录文件。
问题
- 是否有人遇到过类似情况:PyCharm运行单个测试时无法识别根目录conftest.py的Fixtures,但终端运行正常?
- 如何修复该问题,让PyCharm的运行按钮和命令行Pytest都能一致正常工作?
环境信息
- Pytest版本:7.4.4
- PyCharm版本:2023.2.1
- Python版本:3.9.6
内容的提问来源于stack exchange,提问作者llRub3Nll
相关产品推荐
相关产品推荐

