Jenkins(六)
2019-09-14 本文已影响0人
测试游记
欢迎关注我公众号呀~「测试游记」「zx94_11」
Python隔离环境搭建
- 安装Pyenv Pipeline插件
- 在Jenkins机器上安装python,pip,virtualenv
⚠️由于使用虚拟环境搭建,所以没有第三方的库,如果需要使用请使用pip来进行安装
导出现在环境的第三方库
pip freeze > 「xxxx.txt」
批量安装第三方库:
pip install -r 「xxxx.txt」


在流水线中使用Pyenv Pipeline插件提供的withPythonEnv
方法
小括号内为可执行python路径。流水线会在当前工作空间下创建一个virtualenv环境
大括号内的内容就执行在新建的virtualenv环境下

withPythonEnv('/usr/lib/python3'){
sh 'python --version' //查看python版本
}
Allure报告
- 安装Allure Jenkins插件
- 配置Allure自动安装
- 编写pytest脚本
- 执行
- 查看结果


使用片段生成器辅助步骤的生成

下面是流水线部分
由于只编写了简单的测试脚本,所以只需要安装pytest
和allure-pytest
两个第三方库就可以了
最后使用post-always来进行allure报告的展示
报告的链接图标会展示在该任务中
pipeline{
agent any
stages{
stage('Example'){
steps{
withPythonEnv('/usr/bin/python'){
sh 'python -m pip install pytest '
sh 'python -m pip install allure-pytest'
sh 'python -m pytest -v test_allure.py --alluredir=allure-results'
}
exit 0
}
}
}
post{
always{
allure includeProperties: false, jdk: '', results: [[path: 'allure-results']]
}
}
}
pytest脚本:
import pytest
def test_success():
"""this test succeeds"""
assert True
def test_failure():
"""this test fails"""
assert False
def test_skip():
"""this test is skipped"""
pytest.skip('for a reason!')
def test_broken():
raise Exception('oops')
虚拟环境中正在安装第三方库

使用pytest进行测试,并输出报告至allure-results

报告的链接图标⬇️⬇️⬇️

具体的报告⬇️

小结
综上,
在执行的设备上搭建了python,pip,virtualenv环境
在Jenkins上配置了自动安装Allure
完成了环境隔离,测试执行,报告展示