Jenkins(六)

Jenkins(六)

Python隔离环境搭建

  1. 安装Pyenv Pipeline插件
  2. 在Jenkins机器上安装python,pip,virtualenv

⚠️由于使用虚拟环境搭建,所以没有第三方的库,如果需要使用请使用pip来进行安装

导出现在环境的第三方库

pip freeze > 「xxxx.txt」

批量安装第三方库:

pip install -r 「xxxx.txt」

第三方库

插件安装

在流水线中使用Pyenv Pipeline插件提供的withPythonEnv方法

小括号内为可执行python路径。流水线会在当前工作空间下创建一个virtualenv环境

大括号内的内容就执行在新建的virtualenv环境下

python3路径

1
2
3
withPythonEnv('/usr/lib/python3'){
sh 'python --version' //查看python版本
}

Allure报告

  1. 安装Allure Jenkins插件
  2. 配置Allure自动安装
  3. 编写pytest脚本
  4. 执行
  5. 查看结果

安装插件

image-20190713142000087

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

自动生成

下面是流水线部分

由于只编写了简单的测试脚本,所以只需要安装pytestallure-pytest两个第三方库就可以了

最后使用post-always来进行allure报告的展示

报告的链接图标会展示在该任务中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

测试部分

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

测试结果

具体的报告⬇️

Allure报告

小结

综上,

在执行的设备上搭建了python,pip,virtualenv环境

在Jenkins上配置了自动安装Allure

完成了环境隔离测试执行报告展示

 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
您的支持将鼓励我继续创作!