美文网首页软件测试
pytest+Allure生成测试报告

pytest+Allure生成测试报告

作者: 梵音11 | 来源:发表于2020-07-11 21:45 被阅读0次


简介:

  python 主流自动化测试报告插件有三个:HTMLTestRunner、BeautifulReport 和 Allure。HTMLTestRunner是一个比较古老的报告模板,界面也不是很好看。BeautifulReport  界面很简洁,看起来也很直观,是一款比较不错的报告插件。

Allure 是一款轻量级的开源自动化测试报告生成框架。它支持绝大部分测试框架,比如 TestNG、Junit 、pytest、unittest 等。本文主要介绍 pytest 框架结合 Allure 生成 格式统一、美观的 测试报告。

1.Allure 下载安装

Allure 下载最新版本:https://github.com/allure-framework/allure2/releases

下载完成之后,解压到 pytest 目录中。然后设置环境变量,简单一点就是进入 \allure-2.13.0\bin 目录执行 allure.bat 。cmd 输入 allure 查看环境变量是否设置成功。

2. allure-pytest

下载 allure-pytest 插件,用来生成 Allure 测试报告所需要的数据。

pip3 install allure-pytest

3.案例分析:

1.编写一段使用 pytest 框架的测试代码:

#!/usr/bin/env python

# coding=utf-8

import pytest

import allure

import os

@pytest.fixture(scope='function')

def login():

    print("登录")

    yield

    print("登录完成")

@allure.feature('加入购物车')

def test_1(login):

    '''将苹果加入购物车'''

    print("测试用例1")

@allure.feature('加入购物车')

def test_2():

    '''将橘子加入购物车'''

    print("测试用例2")

if __name__ =="__main__":

    # 执行pytest单元测试,生成 Allure 报告需要的数据存在 /temp 目录

    pytest.main(['--alluredir', './temp'])

    # 执行命令 allure generate ./temp -o ./report --clean ,生成测试报告

    os.system('allure generate ./temp -o ./report --clean') 

@allure 装饰器中的一些功能点:

@allure.feature :用于定义被测试的功能,被测产品的需求点

@allure.story : 用于定义被测功能的用户场景,即子功能点

@allure.step :用于将一个测试用例,分成几个步骤在报告中输出

allure.attach : 用于向测试报告中输入一些附加的信息,通常是一些测试数据信息

2.执行后生成 Allure 报告:

打开 index.html ,测试报告如下:

创建了一个测试交流群,如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加测试交流群:829792258,还会有同行一起技术交流

相关文章

网友评论

    本文标题:pytest+Allure生成测试报告

    本文链接:https://www.haomeiwen.com/subject/ehkwcktx.html