配置Hatch使用pytest-cases遇模块找不到问题的解决方法
问题:Hatch测试环境引入pytest-cases后无法运行测试
环境配置
我的pyproject.toml中测试环境配置如下:
# test environment # inherits from default by default [tool.hatch.envs.tests] type = "virtual" path = "/Users/username/.venvs/project_tests" # default dependencies for hatch-test but pycharm doesn't get along with hatch so i'm making it myself dependencies = [ "coverage-enable-subprocess==1.0", "coverage[toml]~=7.4", "pytest~=8.1", "pytest-mock~=3.12", "pytest-randomly~=3.15", "pytest-rerunfailures~=14.0", "pytest-xdist[psutil]~=3.5", "pytest-cases" ]
注:因PyCharm与Hatch兼容问题,我自行创建虚拟环境,包含Hatch文档中所有默认依赖并指定环境路径。
执行命令与错误表现
我执行的测试命令:
hatch test -vv --cover
此前该命令运行正常,但引入pytest-cases后出现如下错误:
tests/test_lexicon_builder_cases.py:3: in <module> from pytest_cases import fixture, parametrize E ModuleNotFoundError: No module named 'pytest_cases'
同时测试会话启动时,pytest-cases未出现在插件列表中:
============================================================ test session starts ============================================================ platform darwin -- Python 3.13.1, pytest-8.3.5, pluggy-1.5.0 -- /Users/username/Library/Application Support/hatch/env/virtual/project/ycq7iGLa/hatch-test.py3.13/bin/python3 cachedir: .pytest_cache rootdir: /Users/username/Documents/project configfile: pyproject.toml plugins: rerunfailures-14.0, mock-3.14.0, xdist-3.6.1 collected 0 items / 1 error
对比情况(直接使用coverage命令正常)
直接调用coverage命令可正常运行测试,且pytest-cases插件会显示在列表中:
执行命令:
coverage run -m pytest tests
测试输出:
============================================================ test session starts ============================================================ platform darwin -- Python 3.13.1, pytest-8.3.5, pluggy-1.6.0 Using --randomly-seed=8972830 rootdir: /Users/username/project configfile: pyproject.toml plugins: randomly-3.16.0, anyio-4.9.0, cases-3.9.1, rerunfailures-14.0, mock-3.14.0, xdist-3.6.1 collected 5 items test_file.py ..... [100%] ============================================================= 5 passed in 0.11s =============================================================
但该方式无法输出我需要的覆盖率报告,我更希望继续使用hatch test -vv --cover命令。
求助
请问配置中缺少什么才能让该命令正常工作?
内容的提问来源于stack exchange,提问作者Liz C.
相关产品推荐
相关产品推荐

