Python 解决 pytest 多线程插件 pytest-parallel 不能和测试报告插件 allure-pytest 兼容的问题

test · 2022年03月25日 · 最后由 test 回复于 2022年10月21日 · 4182 次阅读

背景

在用 pytes 做自动化测试的时候,有时候我们的用例会非常多,可能每个用例又会有 IO 等待。如果顺序运行的话,就会很耗时。所以希望可以并发运行用例。这时可以考虑用多线程插件 pytest-parallel,它可以比 pytest-xdist 多进程版本有更高的并发。但是 pytest-parallel 无法和测试报告插件 allure-pytest 一起使用。它会导致 allure-pytest 无法正常生成测试报告。所以写了这个 pytest-multithreading-allure 插件兼容这个问题

使用示例

allure-pytest 和 pytest-parallel 网上都有很多教程,这里不做介绍,直接贴一个简单的代码实例

import time

import pytest
import allure

@allure.title("测试用例1")
def test_a_01():
    print("----------------->>> test_a_01")
    time.sleep(1)
    assert 1

@allure.title("测试用例2")
def test_a_02():
    print("----------------->>> test_a_02")
    time.sleep(1)
    assert 1


if __name__ == "__main__":
    pytest.main(["-s", "test_allure.py","--tests-per-worker=2","--alluredir", "report/"])

没安装 pytest-multithreading-allure 插件之前运行这个代码,不会在 report 文件夹生成 allure 的测试报告。安装 pip install pytest-multithreading-allure 插件后,allure 的使用方式不变。再次运行上面的代码,可以在 report 文件夹生成测试报告了

共收到 9 条回复 时间 点赞

请问,可以在哪里下载。之前也遇到同样问题。

已解决,指定 aliyun 的镜像下载成功了。之前默认公司的镜像找不到
pip install -i https://mirrors.aliyun.com/pypi/simple/ pytest-multithreading-allure

你好,装了插件以后,alllure.dynamic.title alllure.dynamic.description 好像都不生效了,报告里的 title 和 description 没法改变

我的格式也不生效了

取消了 threading.local() 后,会导致 allure 的一些装饰器失效啊兄弟,这块现在修复了么

为什么可以收集到用例但是没有用例运行
Python3.8.10
allure-pytest 2.11.1
pytest-multithreading 1.0.2
pytest-multithreading-allure 1.0.5
pytest-parallel 0.0.10

(venv) PS C:\Users\hli\Documents\Python\demo> pytest --tests-per-worker 20 --alluredir report 
============================================================================================================= test session starts ==============================================================================================================
platform win32 -- Python 3.8.10, pytest-7.1.3, pluggy-1.0.0
rootdir: C:\Users\hli\Documents\Python\demo
plugins: allure-pytest-2.11.1, multithreading-1.0.2, multithreading-allure-1.0.5, parallel-0.0.10
collected 20 items
pytest-parallel: 1 worker (process), 20 tests per worker (threads)

============================================================================================================ no tests ran in 0.03s ============================================================================================================= 
test #7 · 2022年10月21日 Author
flipped895 回复

嗨,这个现在改好了,升级就行了,pip install --upgrade pytest-multithreading-allure

8楼 已删除
test #9 · 2022年10月21日 Author
HLi 回复

你这个现在还有问题吗?这个看起来和插件没什么关系的

test #10 · 2022年10月21日 Author
comma 回复

这个现在改好了,升级就行了,pip install --upgrade pytest-multithreading-allure

需要 登录 后方可回复, 如果你还没有账号请点击这里 注册