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

pytest将命令行参数值误判为测试路径的错误排查求助

问题原因

你的--server_image参数在定义时使用了action="store_true",这个action的作用是将参数标记为布尔开关:只要命令行中出现该参数,就将其值设为True,不接受后续的参数值。所以当你执行python -m pytest tests --server_image default时,pytest会把default当成额外的测试目标(目录/文件)去查找,自然找不到,抛出file or directory not found: default的错误。

解决办法

将--server_image的action改为"store",这样它就能接收字符串类型的参数值了。修改后的conftest.py代码如下:

def pytest_addoption(parser):
    parser.addoption("--server_image", action="store", default="build_image")
    parser.addoption("--other_attr", action="store_true", default="True")
    ...

修改后,再执行python -m pytest tests --server_image default,pytest就会正确把default解析为--server_image的参数值,而不是测试目标。

另外注意:--other_attr使用action="store_true"时,default="True"的写法不太规范——store_true类型参数的默认值本身是False,如果需要默认开启,直接将default设为True即可,但该参数本身是布尔类型,不需要传递值。如果--other_attr实际需要接收字符串值,那也要改成action="store",不过根据你描述“不传递参数时运行正常”,当前--other_attr的配置应该符合你的需求。

内容的提问来源于stack exchange,提问作者Andrey Kite Gorin

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.20 05:55:23