微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JENKINS使用DOCKER运行PYTEST并且出ALLURE报告

背景

最近想做一个简单的pytest 测试,用allure出报告,结果发现网上的方法都是在windows上装jenkins,然后用jenkins跑一个本地的运行环境。这种做法明显很不2019年。于是我决定做一个在jenkins上使用docker运行pytest,然后再出allure报告的文章

思路

  1. 在一台电脑上安装jenkins,可以参考我的文章https://www.yinyubo.cn/?p=268
  2. 准备python代码,放到git上,jenkins通过git拉取代码代码中包含Dockerfile,测试用例,requirements.txt
  3. 拉取代码之后,通过Dockerfile产生一个python的镜像,并且在镜像中通过requirements.txt去安装pytest,allure的相关依赖
  4. 通过这个镜像,产生一个容器,容器命令运行pytest,产生测试报告,并且挂载到jenkins服务器上.
  5. (注意,这里为什么要在容器里运行pytest呢?因为如果在Dockerfile里运行pytest,一旦产生测试失败,是会导致jenkins退出shell执行的)
  6. Jenkins通过allure插件读取第4步产生的测试报告。生成allure报告

具体步骤

  1. 第一步忽略。请参考文章https://www.yinyubo.cn/?p=268
  2. 准备python代码。产生Dockerfile,测试用例,requirements.txt,如下图

Dockerfile内容如下:

 

1

2

3

4

5

FROM python:3.7.3

workdir .

ADD . .

RUN pip install -r requirements.txt

CMD ["pytest", "-q","TestCase/aimP/Connect/aimP_Connect_01.py","--alluredir","allure-results"]

requirements.txt内容如下:

 

1

2

3

4

5

6

7

8

9

10

allure-pytest==2.6.2

allure-python-commons==2.6.2

atomicwrites==1.3.0

attrs==19.1.0

colorama==0.4.1

more-itertools==7.0.0

pluggy==0.9.0

py==1.8.0

pytest==4.4.1

six==1.12.0

测试用例可以如下

 

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

import pytest

def test_success():

    """this test succeeds"""

    print(1)

    assert True

 

 

def test_failure():

    print(2)

    """this test fails"""

    assert False

 

 

def test_skip():

    print(3)

    """this test is skipped"""

    pytest.skip('for a reason!')

 

 

def test_broken():

    raise Exception('oops')

 

def test_success2():

    print(4)

    assert True

     3.jenkins配置,这里如何拉取git代码就不写了。这个很简单,主要是构建命令和构建后操作,这里我复制了我的构建命令如下:

 

1

2

3

name="apitest"

docker build -t $name .

docker run -d --name $name -v $PWD/allure-results:/allure-results $name

效果如图:

    4.运行job,查看allure结果

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐