Jenkins(六)

Python隔离环境搭建

  1. 安装Pyenv Pipeline插件

  2. 在Jenkins机器上安装python,pip,virtualenv

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

导出现在环境的第三方库

pip freeze > 「xxxx.txt」

批量安装第三方库:

pip install -r 「xxxx.txt」

640?wx_fmt=jpeg
第三方库
640?wx_fmt=jpeg
插件安装

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

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

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

640?wx_fmt=jpeg
python3路径
withPythonEnv('/usr/lib/python3'){
   sh 'python --version' //查看python版本
}

Allure报告

  1. 安装Allure Jenkins插件

  2. 配置Allure自动安装

  3. 编写pytest脚本

  4. 执行

  5. 查看结果

640?wx_fmt=jpeg
安装插件
640?wx_fmt=jpeg
image-20190713142000087

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

640?wx_fmt=jpeg
自动生成

下面是流水线部分

由于只编写了简单的测试脚本,所以只需要安装pytestallure-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')

虚拟环境中正在安装第三方库

640?wx_fmt=jpeg
环境部分

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

640?wx_fmt=jpeg
测试部分

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

640?wx_fmt=jpeg
测试结果

具体的报告⬇️

640?wx_fmt=jpeg
Allure报告

小结

综上,

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

在Jenkins上配置了自动安装Allure

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值