pytest--allure

Allure 是一款轻量级的开源自动化测试报告生成框架。它支持绝大部分测试框架,比如 TestNG、Junit 、pytest、unittest 等。
 
1.Allure 下载安装
下载完成之后,解压到 pytest 目录中。然后设置环境变量,简单一点就是进入 allure-2.13.0in 目录执行 allure.bat 。
cmd 输入 allure 查看环境变量是否设置成功。
 
2. allure-pytest
下载 allure-pytest 插件,用来生成 Allure 测试报告所需要的数据。
pip3 install allure-pytest
 
案例分析:
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 ,测试报告如下: chrome打开后报404,尝试用火狐,edge试试~
 
 

原文地址:https://www.cnblogs.com/absoluteli/p/13984894.html