You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决pytest无法忽略tests目录下legacy文件夹的问题?

解决pytest忽略指定目录的配置问题

针对你的测试结构,问题出在norecursedirs的路径写法上,修正后的pyproject.toml配置如下:

[tool.pytest.ini_options]
testpaths = "tests"
norecursedirs = "legacy"

为什么原来的配置不生效?

你之前写的norecursedirs = "tests/legacy/*"是匹配legacy目录下的所有子项,但norecursedirs的作用是跳过指定的目录本身,而不是目录里的内容。加上/*后,pytest无法匹配到legacy这个目录,所以依然会遍历其中的测试文件。

因为你已经设置了testpaths = "tests",这里直接使用相对路径legacy就足够精准,pytest会自动在tests目录下跳过这个子目录。

如果需要更明确的路径写法,也可以写成:

[tool.pytest.ini_options]
testpaths = "tests"
norecursedirs = "tests/legacy"

两种写法都能让pytest命令完全忽略legacy文件夹下的所有测试文件,满足你无需额外参数、直接运行pytest的需求。

内容的提问来源于stack exchange,提问作者Pierrick Rambaud

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:46:01