环境搭建
Jenkins安装
Jenkins的安装我在:https://blog.51cto.com/u_15305635/3673705
这篇文章中已经详细写出,直接参考即可
Allure安装
1.安装
常见两种方式:
方式一:
pip install allure-pytest
方式二:
下载allure的安装包,解压将allure路径配置在环境变量中
下载:
链接:https://pan.baidu.com/s/16ow0uDnNfrmKfAaZtJ9Vow
提取码:6666
环境变量path添加:E:\***(改成自己的)\allure-2.14.0\bin
2. 验证
# 安装后查看版本
allure --version
Jenkins+allure插件安装
1.安装
步骤:Manage Jenkins—>Manage Plugins —>可选插件—>输入:allure—>回车—>会自动加载查询结果,然后选择安装,安装成功后,重启Jenkins服务;
2.验证
步骤:Manage Jenkins—>Manage Plugins —>已安装—>可以看到刚才安装的插件;
这一步我们就完成了插件的安装;
3.全局配置
3.1 步骤:Manage Jenkins—>Global Tool Configuration—>Allure Commandline
3.2 点击Allure Commandline 安装
3.2.1 输入别名
3.2.2 配置allure的安装目录
3.2.3 保存
新建项目
1.新建项目及设置
步骤:
新建Item—>录入项目名称 :demp_a —> 风格选择:Freestyle project—>确定—>General—>使用自定义的工作空间—>显示名称
备注:
项目名称与风格根据自己实际需求设置,这里做demo演示,考虑面相对不广;
使用自定义的工作空间:这个根据自己需求设置
2.构建
这部分就是Jenkins在build的时候会执行的命令;
我的是Windows环境,就选择执行Windows批处理命令了:Excute Windows batch command,再输入命令:
cd E:\java_python\work_code\tmp\pytest_demo
pytest -s -q --alluredir=pytest_demo/allure-results --clean-alluredir
exit 0
命令分为三部分:
第一行:切换工作目录,这个目录下存放了一个基于Pytest的文件test_demo.py
(文件具体内容在文末自取);
第二行:执行Pytest命令;
第三行:批处理的退出命令;
3.构建后操作
Jenkins在构建完成,执行Pytest命令会生成响应的测试数据(json格式)
只需要设置path即可,和刚才命令pytest -s -q --alluredir=pytest_demo/allure-results --clean-alluredir
中的alluredir
一致即可将path设置成pytest_demo/allure-results
;
设置结束,保存退出
Build
回到项目的首页—>build now
状态解释:
分别对上面3出标记做简单解释:
1.触发build now后,项目会进到build阶段进度条会显示当前build的进度;
2.如果项目是基于allure生成报告的,会在后面添加allure的标志;
3.build异常
报告查看
点击小图标就能进去查看报告
![在这里插入图片描述](https://img-blog.csdnimg.cn/b9bcc2347c224dfa9d3378e14ffe68b1.png
报告展示:
overview:
Behavior:
遇到问题
- ru.yandex.qatools.allure.jenkins.exception.AllurePluginException: Can not find any allure commandline installation
Error when executing always post condition:
ru.yandex.qatools.allure.jenkins.exception.AllurePluginException: Can not find any allure commandline installation.
at ru.yandex.qatools.allure.jenkins.AllureReportPublisher.getCommandline(AllureReportPublisher.java:349)
at ru.yandex.qatools.allure.jenkins.AllureReportPublisher.generateReport(AllureReportPublisher.java:303)
at ru.yandex.qatools.allure.jenkins.AllureReportPublisher.perform(AllureReportPublisher.java:231)
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:80)
at org.jenkinsci.plugins.workflow.steps.CoreStep$Execution.run(CoreStep.java:67)
at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
解决方案:
a.有没有安装并设置环境变量allure;
b.有没有在Allure Commandline 安装
设置全局allure目录;
- java.io.IOException: Can’t find allure commandline
解决方案:
a.Allure Commandline 安装
设置全局allure目录
allure的安装目录,不要设置成:E:\ws\allure-2.14.0\bin
,要设置成E:\ws\allure-2.14.0
附录:
test_demo.py
import pytest
@pytest.fixture(scope='module') #名为login的fixture,完成登陆和注册操作
def login():
print('登陆操作~~~')
yield
print('注销操作~~~')
@pytest.mark.usefixtures("login")
class TestClass1:
def test_case1(self):
print("TestClass1:test_case1")
def test_case2(self):
print("TestClass1:test_case2")
class TestClass2:
def test_case1(self):
print("TestClass2:test_case1")
def test_case2(self):
print("TestClass2:test_case2")
到这就基本介绍了Jenkins+pytest+allure接口自动化测试的一部分内容了,