新手区 python+allure+jenkins

loshu2003 · 2017年10月13日 · 最后由 回复于 2022年04月12日 · 3234 次阅读

前言

之前用 python 写接口测试脚本一直用的是 HTMLTestRunner,后来发现 Testerhome 上有介绍 Allure,感觉 allure 展示的测试报告非常酷炫,所以试着将 allure 集成到测试中,在这里记录一下用做备忘。

在 python 中使用 allure

在 python 中使用 allure 需要使用 pytest 和 pytest-allure-adaptor,所以先使用 pip 安装它们俩。

1. 安装 pytest

pip install pytest

2. 安装 pytest-allure-adaptor

pip install pytest-allure-adaptor

3. 使用 pytest 执行测试用例并生成 allure 中间报告(此步骤可以省略,因为在 jenkins job 中会配置执行类似的命令)

  • 我的脚本目录结构

  • 在测试脚本根目录执行以下命令,跑 test_zutu_seacher.py 中的 case
python -m pytest cases/zutu_searcher/ --alluredir report

以上命令要在 pytest 的前面加上 python -m,因为如果不加会报如下的错误。可以参考

=========================================================================== test session starts ============================================================================
platform linux2 -- Python 2.7.9, pytest-3.2.3, py-1.4.34, pluggy-0.4.0
rootdir: /da1/jenkins/auto_testimg, inifile:
plugins: allure-adaptor-1.7.8
collected 0 items / 1 errors                                                                                                                                                

================================================================================== ERRORS ==================================================================================
________________________________________________________ ERROR collecting cases/zutu_searcher/test_zutu_seacher.py _________________________________________________________
ImportError while importing test module '/da1/jenkins/auto_testimg/cases/zutu_searcher/test_zutu_seacher.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
cases/zutu_searcher/test_zutu_seacher.py:5: in <module>
    from common import commonMethod
E   ImportError: No module named common
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
========================================================================= 1 error in 0.10 seconds =========================================================================

这个时候脚本根目录的 report 目录下生成了几个文件,类似下面的文件,但是还不是 allure 最终的测试报告,还需要和 jenkins 的Allure Jenkins Plugin配合生成最终的测试报告。

[xieyuchang@test1 /da1/jenkins/auto_testimg]$ ll report
total 28
-rw-rw-r-- 1 xieyuchang xieyuchang  113 Oct 13 11:20 1a65faa2-2932-436a-bf7b-f9cb395965ab-attachment.txt
-rw-rw-r-- 1 xieyuchang xieyuchang 2708 Oct 13 11:20 1f1f0d8b-4dcd-44a9-9b92-e70e3ee2d948-testsuite.xml
-rw-rw-r-- 1 xieyuchang xieyuchang  403 Oct 13 11:20 8e7754ca-359f-41ef-8bd1-072dbae6ccf6-attachment.txt
-rw-rw-r-- 1 xieyuchang xieyuchang  605 Oct 13 11:20 d307ad23-4465-4764-bf00-439d03c67ccc-attachment.txt
-rw-rw-r-- 1 xieyuchang xieyuchang  278 Oct 13 11:20 e2309b59-627d-47fc-a8c6-a1b9894620a5-attachment.txt
-rw-rw-r-- 1 xieyuchang xieyuchang  161 Oct 13 11:20 e297c184-579f-4339-a54a-cc94828cae12-attachment.txt
-rw-rw-r-- 1 xieyuchang xieyuchang  283 Oct 13 11:20 e7206875-91a8-41cb-acf4-01ab6f85707d-attachment.txt

4. Jenkins 中安装Allure Jenkins Plugin

安装 Allure Jenkins Plugin 和安装其它 jenkins 插件一样,不再赘述。

5. Jenkins 中配置 Allure Commandline

[系统管理]->[Global Tool Configuration] 配置如下

6. Jenkins 执行测试用例 Job 配置

注意命令:python -m pytest --alluredir ${WORKSPACE}/report 中指定的生成报告的目录和 Allure Report 中配置的目录是同一个目录,否则最终报告是空白的。

7. 执行 job 查看测试报告

  • allure 测试报告入口

  • allure 测试报告

共收到 15 条回复 时间 点赞

为什么我用 pytest 执行脚本会没有运行呢


改成 python -m pytest 也不行

如果 jenkins 部署在远程,跟你的项目不在一台服务器上,那该怎么搞?

MissA 回复

就是 Jenkins 在你当前 workspace 找不到 pytest 执行后的结果,所以工作目录生成的报告是空的,你需要把 pytest 执行后的结果存在当前 workspace 下,然后才能找到生成对应的报告

我的 Allure Report 点击显示 404 错误是什么问题?

17楼 已删除

请问楼主,如何加截图和 URL

您好,对于提示 allure-results does not exists 这个提示,最后怎么解决的?
能 详细表述下?

求问,allure 报告里的历史报告和返回 jenkins 网页都显示 404 是什么原因

你填的 report 目录要是你 job 的 workspace 下的目录,看你的截图应该不是

匿名 #11 · 2018年04月13日

楼主,配置中,构建和构建后操作我是这样的,可是结果在生成报告时,提示我 allure-results does not exists,是什么原因,是要配置什么参数么?

loshu2003 重新开启了讨论 03月30日 10:46
loshu2003 关闭了讨论 03月30日 10:46
loshu2003 重新开启了讨论 03月30日 10:43
loshu2003 关闭了讨论 01月25日 09:14

--alluredir 是一个参数

匿名 #5 · 2017年11月21日

还有 python -m pytest 测试用例文件夹路径 --alluredir allure-result 这样写的话无法执行,0 个用例被执行(collected 0 items),但是如果是把测试用例文件夹路径改为具体的测试用例的路径就可以了 不知道楼主知道原因吗?

---解决了,因为 py 文件的命名问题

匿名 #20 · 2017年11月21日

楼主你好。想问问一下,--alluredir 是什么意思?是一个预先在哪里设定的值吗? 还是仅仅是个命令参数
感谢回答 谢谢!

loshu2003 回复

感谢 还可以通过

通过配置文件配置每条用例的执行次数,新手分享哈

chma 回复

可以用下面的代码试试

  • test_hello.py 文件内容
import pytest

@user1ize('execution_number', range(3))
def test_run_multiple_times(execution_number):
    print 'hello'
    assert True
  • 执行
pytest test_hello.py

请教一个问题 怎么在 pytest 里面设置用例的执行次数 (不同用例重复执行次数不一样)

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