pytest并发执行用例方案

背景

开始做新项目的UI自动化,需要考虑用例的并发执行,因为之前做的项目是通过插件pytest-parallel 0.1.1 + pytest-multithreading-allure 1.0.8来实现的,所以这次也打算用此方法,然而在实际使用过程中发现一些问题。

问题一

通过 pytest --workers n 运行后出现报错,错误是:AttributeError: Can't pickle local object 'pytest_addoption.<locals>.label_type.<locals>.a_label_type'
解决方法:由于运行环境是windows,查资料windows上要安装0.0.10版本, 之前都是在mac或者Linux环境上运行没有遇到此问题

问题二

pytest-parallel降级为版本0.0.10,再次运行,还是会报错,但错误是:

复制代码
............
AttributeError: '_Environ' object has no attribute 'putenv'

解决方法 : 参考https://github.com/kevlened/pytest-parallel/issues/89#issuecomment-797698926进行修改,修改后执行 pytest --tests-per-worker n 可以多线程运行, window上pytest --workers n 时,n只能是1

总结

  1. 经过上面的折腾终于解决了windows上使用pytest-parallel的问题,但是始终要去修改pytest-parallel中的内容,决定改为使用pytest-xdist,安装最新版本的pytest-xdist , 安装后执行 pytest -n n即可 , 发现pytest-xdist会导致scope=session的fixture设置失效,即导致每个用例都会执行一次此fixture,正常情况是只需要执行一次的,详见session失效的解决办法
  2. 如果运行环境是Linux或者mac,应该还是会优先选择pytest-parallel
相关推荐
清水白石00828 分钟前
《解锁 Python 潜能:从异步基石到 pytest-asyncio 高级测试实战与最佳实践》
运维·python·pytest
清水白石0082 天前
Fixture 的力量:pytest fixture 如何重新定义测试数据管理
数据库·python·pytest
七夜zippoe3 天前
单元测试进阶:pytest高级特性与实战秘籍
单元测试·pytest·持续集成·猴子补丁·fixtrue
我的xiaodoujiao7 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 48--本地环境部署Jenkins服务
python·学习·测试工具·pytest
我的xiaodoujiao7 天前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 49--CI/CD-开始探索使用Jenkins
python·学习·测试工具·ci/cd·jenkins·pytest
测试渣9 天前
测试左移实战:如何在需求阶段拦截80%缺陷?
selenium·pytest·测试覆盖率
一次旅行9 天前
测开每日AI提效指令(Python+pytest专属)
python·pytest·测试总结
一次旅行9 天前
接口自动化测试模板
数据库·python·pytest
Kingairy10 天前
Claude Python3+pytest Code Review skill.md
pytest·代码复审
姚青&11 天前
Pytest 参数化与调度执行
pytest