pytest.ini 配置详解:将选项参数写入 pytest.main() 函数
pytest.ini 配置详解:将选项参数写入 pytest.main() 函数
在 pytest 中,您可以使用 pytest.ini 文件来配置测试运行方式。例如,您可以使用以下配置来设置测试运行选项:
[pytest]
addopts = -vs --reruns 1 --reruns-delay 1 --only-rerun NoWaveFileError -p no:warnings --color yes --continue-on-collection-errors
如果您想在代码中使用这些选项,您可以将它们作为参数传递给 pytest.main() 函数。以下是示例:
pytest.main(['-vs', '--reruns', '1', '--reruns-delay', '1', '--only-rerun', 'NoWaveFileError', '-p', 'no:warnings', '--color', 'yes', '--continue-on-collection-errors'])
其中,pytest.main() 函数的参数是一个列表,列表中的每个元素都是一个命令行参数。需要注意的是,命令行参数之间要用逗号隔开,而不是空格。
注意:
- 使用
pytest.main()函数运行测试时,会覆盖pytest.ini文件中的配置选项。 - 建议在
pytest.ini文件中配置通用选项,在代码中使用pytest.main()函数配置特定于测试用例的选项。
总结:
通过将 pytest.ini 文件中的配置参数写入 pytest.main() 函数,您可以灵活地控制测试运行方式,并根据需要调整测试配置。
原文地址: https://www.cveoy.top/t/topic/ofPb 著作权归作者所有。请勿转载和采集!