python+allure+jenkins接口自动化测试框架搭建

Posted 程序员技术前沿

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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]配置如下

python+allure+jenkins接口自动化测试框架搭建


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

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

python+allure+jenkins接口自动化测试框架搭建


7. 执行job查看测试报告

  • allure测试报告入口

    python+allure+jenkins接口自动化测试框架搭建

  • allure测试报告


以上是关于python+allure+jenkins接口自动化测试框架搭建的主要内容,如果未能解决你的问题,请参考以下文章

Jenkins上实现Python + Jenkins + Allure Report 接口自动化测试持续集成

接口自动化CI持续集成之jenkins(docker安装jenkins+python+pytest+allure)

python+requests+pytest+yaml/json+Allure+jenkins+docker接口自动化框架保姆级教学

python数据驱动+接口自动化测试pytest+allure+yaml+jenkins+git(gitlab/gitee)下的接口自动化测试实战

Docker + Jenkins+ Git + Python + allure的曲折历程《一》

Jenkins+python+allure-pytest之API自动化测试框架