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

如何在pytest.ini中设置pytest别名以运行指定测试?

配置pytest.ini实现pytest end2end映射到pytest tests/end2end/*.py

方法1:利用pytest内置路径查找逻辑(最简单)

如果你的测试文件遵循test_*.py或*_test.py的命名规范,只需在项目根目录的pytest.ini中添加如下配置:

[pytest]
testpaths = tests

此时运行pytest end2end,pytest会自动在tests目录下定位end2end子目录,并执行其中所有符合规则的测试文件,效果等价于pytest tests/end2end/。

若之前配置testpaths未生效,大概率是以下原因:

  • end2end目录下的文件不符合pytest测试文件命名要求
  • testpaths配置的路径有误(比如写成test而非tests)
  • 未在项目根目录运行pytest(可通过pytest --rootdir .指定根目录验证)

方法2:通过conftest.py自定义参数映射(精确匹配*.py)

如果需要严格等价于pytest tests/end2end/*.py(仅执行该目录下的py文件,不递归子目录),可在项目根目录创建conftest.py文件,添加以下代码:

def pytest_configure(config):
    if "end2end" in config.args:
        config.args.remove("end2end")
        config.args.append("tests/end2end/*.py")

运行pytest end2end时,pytest会自动将参数替换为tests/end2end/*.py,完全满足需求。

注意事项

  • 方法2支持搭配其他参数使用(如pytest end2end -v),只会替换end2end参数
  • 确保tests/end2end路径正确,若目录结构不同,修改代码中的路径即可

内容的提问来源于stack exchange,提问作者Ons Elleuch

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 21:25:26