首页/开发/软件测试速查/pytest 配置文件
自动化测试66 条

pytest 配置文件

pytest 配置集中在 pytest.ini、pyproject.toml 的 [tool.pytest.ini_options] 或 tox.ini;常用项包括 testpaths、addopts、markers、filterwarnings 与日志设置。

软件测试速查开发更新于 2026-10-12

pyproject 示例

pyproject 示例
[tool.pytest.ini_options]
testpaths = ["tests"]
addopts = "-q -ra --strict-markers"
markers = [
    "smoke: 冒烟集合",
    "slow: 慢用例",
]
filterwarnings = ["error"]
把约定参数(-ra、--strict-markers)放进 addopts,让本地与 CI 行为一致。

易错点

  • 多个配置文件并存时,实际生效的不是正在编辑的那一个
  • 把环境相关参数写进 addopts,本地与 CI 互相打架

数据来源与参考

  • pytest 官方文档:Configuration
​‌‌​​‌​​​‌‌​‌‌‌‌​‌‌​​​‌‌​‌‌‌​​‌‌​​‌​‌‌‌​​‌‌‌​​​‌​‌‌​‌​​‌​‌‌‌​​​‌​‌‌​‌​​‌​‌‌​​​‌‌​‌‌‌​‌​​​‌‌​‌‌‌‌​​‌​‌‌‌​​‌‌​​​‌‌​‌‌​‌‌‌‌​‌‌​‌‌​‌