python+allure+jenkins

目录

前言

[在 python 中使用 allure](#在 python 中使用 allure)

[1. 安装 pytest](#1. 安装 pytest)

[2. 安装 pytest-allure-adaptor](#2. 安装 pytest-allure-adaptor)

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

[4. Jenkins 中安装Allure Jenkins Plugin](#4. Jenkins 中安装Allure Jenkins Plugin)

[5. Jenkins 中配置 Allure Commandline](#5. Jenkins 中配置 Allure Commandline)

[6. Jenkins 执行测试用例 Job 配置](#6. Jenkins 执行测试用例 Job 配置)

[7. 执行 job 查看测试报告](#7. 执行 job 查看测试报告)


前言

使用Python结合Allure和Jenkins是一种强大的组合,可以实现自动化测试和测试报告的生成与展示。

之前用 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\] 配置如下 [![](https://file.jishuzhan.net/article/1681966289662251010/e1666596c1da48a6bb3511ef72bad4b9.png)](https://testerhome.com/uploads/photo/2017/cebeb77e-7ec8-41b0-9e96-c902c123b1c4.png!large) ### 6. Jenkins 执行测试用例 Job 配置 **注意命令:python -m pytest --alluredir ${WORKSPACE}/report 中指定的生成报告的目录和 Allure Report 中配置的目录是同一个目录,否则最终报告是空白的。** [![](https://file.jishuzhan.net/article/1681966289662251010/9e42555c55c74fa2908f33246127b203.png)](https://testerhome.com/uploads/photo/2017/f0dca099-82d7-4b08-97c2-3d6bfd982693.png!large) ### 7. 执行 job 查看测试报告 * allure 测试报告入口 [![](https://file.jishuzhan.net/article/1681966289662251010/99c3a7c13011473bb0e2da144777f224.png)](https://testerhome.com/uploads/photo/2017/6fc192ae-63e3-415d-8a7d-22f12ffe979d.png!large) * allure 测试报告 [![](https://file.jishuzhan.net/article/1681966289662251010/f97dc217c6514f388813efa185961ba7.png)](https://testerhome.com/uploads/photo/2017/d0e891d8-d8aa-4b6e-9f58-d0af74f8f2e6.png!large) ![](https://file.jishuzhan.net/article/1681966289662251010/f2ee40cfd3fb4674b2fc9316916afb2b.gif) 作为一位过来人也是希望大家少走一些弯路 在这里我给大家分享一些自动化测试前进之路的必须品,希望能对你带来帮助。 (WEB自动化测试、app自动化测试、接口自动化测试、持续集成、自动化测试开发、大厂面试真题、简历模板等等) 相信能使你更好的进步! 点击下方小卡片 ![](https://file.jishuzhan.net/article/1681966289662251010/224975d30f3b44ef8e374b50c68b3d2e.gif)

相关推荐
狐凄2 分钟前
Python实例题:Python计算线性代数
开发语言·python·线性代数
西猫雷婶3 分钟前
pytorch基本运算-导数和f-string
人工智能·pytorch·python
述雾学java6 分钟前
深入理解 transforms.Normalize():PyTorch 图像预处理中的关键一步
人工智能·pytorch·python
要努力啊啊啊9 分钟前
使用 Python + SQLAlchemy 创建知识库数据库(SQLite)—— 构建本地知识库系统的基础《一》
数据库·人工智能·python·深度学习·自然语言处理·sqlite
邪恶的贝利亚16 分钟前
从webrtc到janus简介
后端·asp.net·webrtc
Livingbody18 分钟前
Whisper 使用简单实例教程【1】
后端
啾啾Fun18 分钟前
【Java微服务组件】分布式协调P4-一文打通Redisson:从API实战到分布式锁核心源码剖析
java·redis·分布式·微服务·lua·redisson
惊鸿一博35 分钟前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
Bruce_Liuxiaowei39 分钟前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
成工小白40 分钟前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针