代码改变世界

我的pytest系列 -- pytest+allure+jenkins项目实践记录(1)

2020-07-12 18:59  那个杰克  阅读(466)  评论(0编辑  收藏  举报

一次偶然的面试机会,一位面试官给我的启发,为什么不用pytest代替unittest做自动化呢?你应该学学pytest,这个对你有好处!故事就从这里开始,我已经准备好酒了

pytest是什么

摘自:pytest官网
The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.
大致意思是:pytest框架更容易做测试框架,还有一大波好用的插件,大致就是不给你造轮子的机会,在商场里看到什么可以用的就可以拿什么,何乐而不为之,代码的世界真好,但还是要勇敢的面对真实世界☺

pytest的好处

01 更少的"模板"
02 更详细的失败信息
03 更灵活的fixture
04 测试参数化
05 小型的测试套
06 集成CI
07 支持插件
08 活跃的社区
传送门

Allure是什么

摘自官网
Allure Framework is a flexible lightweight multi-language test report tool that not only shows a very concise representation of what have been tested in a neat web report form, but allows everyone participating in the development process to extract maximum of useful information from everyday execution of tests.
Allure是一款开源的,专门用来展示测试结果的工具,目的是希望团队内部的每一个人都可以看到非常清楚的测试报告
Allure可以支持许多的测试工具以及测试框架,下面内容为:Jenkins + pytest框架 + allure
先上个小图:

目录

  • 实现的效果展示
  • pytest的使用
  • allure+pytest如何搭配使用
  • 如何集成到jenkins
  • allure如何显示pytest测试日志

效果展示

pytest的使用
1、使用pytest的参数化
部分代码记录
test_case.py测试文件中,
参数使用pytest.mark.parametrize,直接读取json格式的测试数据对象文件,根据参数化的方式,发起单元测试

@pytest.mark.parametrize(
    "url,method,headers,body,type,assertContent",
    json_return_dict(get_test_data("A_testcase.json")),
    ids=json_return_ids(get_test_data("A_testcase.json"))
)
def test_fun1(url,method,headers,body,type,assertContent):
    logging.info("发起{0}请求,请求路径:{1}".format(method, url))
    if method == "GET":
        req = requests.get(url)
        response = req.content
        logging.info("响应结果:{0}".format(response))
        assert True

allure+pytest如何搭配使用
1、需要安装下载模块
pip install allure-pytest

2、运行测试

运行命令:pytest -s -v -q test_case.py --capture=no --alluredir=./report
./report 表示生成allure报告存放的目录,生成的文件可能看不懂,但需要allure-commandline命令运行就能生成对应的测试报告

下载并安装allure-commandline工具包
使用官网下载安装包传送门1使用npm方式下载传送门2

运行生成好看的报告
命令:allure generate report -o ./allure-result
allure generate report -o ./allure-result --clean
--clean 每次生成清空掉上一次生成的报告记录

三、如何集成到jenkins (配置篇--pipeline方式)

前提是:jenkins需要安装支持allure的插件,步骤:

  1. 在jenkins插件网站上下载allure插件最新版本:http://mirrors.jenkins-ci.org/plugins/allure-jenkins-plugin/
  2. 登录Jenkins,在系统管理-> 插件管理 -> 高级 -> 上传插件,进行安装

    上传成功并安装,看到出现一个黄色的球表示已安装成功,这个时候需要把Jenkins重启即可使用啦

1、构建一个pipeline工程,Jenkins->新建任务->创建一个pipeline工程

2、pipeline脚本编写一个执行pytest的测试步骤,和一个构建完成后的post动作(pipeline的基础语法,小编建议读者可以自行学习Jenkins2-pipeline基础语法)
上代码

pipeline {
    agent any
    stages {
        stage('name1'){
            steps{
                
                bat label: '', script: """
                cd api-collection-test\\trunk
                pytest
                """
            }
            
        }
    }
    post {
        always{
            allure includeProperties: false, jdk: '', results: [[path: 'api-collection-test\\trunk\\report']]
        }
    }
}

解释:

  1. api-collection-test\trunk是我本是jenkins,workspace工作空间的相对的工程目录
  2. api-collection-test\trunk\report是allure生成的原始报告文件的目录,workspace工作空间的相对目录