欢迎访问 生活随笔!

生活随笔

当前位置: 首页 >

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

发布时间:2025/3/11 29 豆豆
生活随笔 收集整理的这篇文章主要介绍了 JENKINS使用DOCKER运行PYTEST并且出ALLURE报告 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

背景

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

思路

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

  • 第一步忽略。请参考文章https://www.yinyubo.cn/?p=268
  • 准备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结果

    总结

    以上是生活随笔为你收集整理的JENKINS使用DOCKER运行PYTEST并且出ALLURE报告的全部内容,希望文章能够帮你解决所遇到的问题。

    如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。