pytest+allure+jenkins,生成allure报告

只谈情不闲聊 提交于 2020-03-17 16:28:01

1、本地生成allure报告

1、安装依赖

allure-2.13.2
allure-pytest-2.8.11
pytest-5.4.1
python-jenkins
jdk-1.8

2、安装后验证安装是否成功

pip list

在这里插入图片描述
在这里插入图片描述

3、配置环境变量

jdk-1.8
pytest-5.4.1
allure-2.13.2

4、编写示例代码Demo

import allure
import pytest


class Test_Pytest():

    @allure.feature("测试成功的用例")
    def test_one(self):
        print("test_one方法执行")
        assert 1 == 1

    @allure.feature("测试失败的用例")
    def test_two(self):
        print("test_two方法执行")
        assert "s" in "love"

    @allure.feature("测试失败的用例")
    def test_three(self):

        print("test_three方法执行")
        assert 3 - 2 != 1

    @allure.feature("测试失败的用例")
    def test_four(self):
        print("test_four方法执行")


if __name__ == "__main__":
    pytest.main(["-s", "-q", "--alluredir", "./report"])

通过命令执行test_Pytest.py,生成allure报告

# 指定report存放位置,结果存放到文件中
# report中生成的是json文件
pytest test_Pytest.py  --alluredir report
# 此命令将report下面的json文件渲染成HTML结果
# clean是将之前生成的文件清空,重新渲染
# 默认HTML保存在allure-report文件夹中
allure generate report --clean

界面展示如下图:
在这里插入图片描述

2、集成到Jenkins中

1、本地安装Jenkins,启动

2、新建item项目:Pytest_allure_demo

3、配置Jenkins:

配置Jenkins全局配置:

注:
配置完后可以用http://localhost:8080/restart重启Jenkins

配置AllureCommandline如下:

可以添加本机的安装目录或者勾选自动安装From Maven Central,然后选择一个版本,如果构建项目过程中报找不到allure,会默认从Maven Central中下载安装一个。
在这里插入图片描述

配置item:

使用自定义的工作空间添加项目目录:我本地项目路径为C:\pythonprojects\oh_pytest\testcases
在这里插入图片描述
构建处新增要执行的批处理脚本:
在这里插入图片描述

c:
pytest test_Pytest.py  --alluredir  ./report/ 
allure generete report --clean
exit 0

注意:
1、构建后操作,results要填写与脚本一致的文件夹名称
2、Archive the artifacts 填写index的路径,我本地的位置也是默认生成的位置是在allure-report\index.html
在这里插入图片描述

jenkins中环境变量的配置:
执行脚本的时候如果报pytest或者“‘allure’ 不是内部或外部命令,也不是可运行的程序或批处理文件。”可以通过设置全局的环境变量解决;如果还不行,需要pip uninstall 这些模块,cmd进入切换到power shell重新安装一下,处理一些环境变量导致的问题。
在这里插入图片描述
在这里插入图片描述

在这里插入图片描述

构建结果:

点击Allure Report可以查看html报告
在这里插入图片描述

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!