Pytest自动化测试

摘要:
除了Python的基本状态之外,Allure几乎支持所有其他函数。它被称为诱惑严重性_作为参数的级别枚举值为BLOCKER、CRITICAL、NORMAL、MINOR和TRIVAL。示例:#test_Sample。pyimportaller#添加两个数字defdd(x,y):returnx+y#测试类@alloure。severityclassTestAdd:@alloure。severitydefest_first:assertadd(3,4)==7@allure.severitydeftest_second:assertadd==1@allure.severitydeftest_three:assertadd==-1@allure.severitydeftest_four:assertadd==-7运行:E:workspace pyPytest˃pytesttest_sample。py--alloredir=报告--clean alloredir=============================================测试会话状态===============================================================平台win32--Python3.7.3,pytest-6.0.2,py-1.9.0,pluggy-0.13.0rootdir:E:工作区pyPytestplugins:allore-pytest-2.8.18,假设-2.3.3,cov-2.10.1,html-3.0.0,reraudulures-9.1.1,xdist-2.1.0collected4itemstest_Sample.py……[100%]========================================================================================================================================报告:还可以使用--all levels选项指定要运行的严重级别。多个级别用逗号分隔。

Allure除了具有Pytest基本状态外,其他几乎所有功能也都支持。


1、严重性

如果你想对测试用例进行严重等级划分,可以使用 @allure.severity 装饰器,它可以应用于函数,方法或整个类。

它以 allure.severity_level 枚举值作为参数,分别为:BLOCKER(中断),CRITICAL(严重),NORMAL(常规),MINOR(轻微),TRIVIAL(不重要)。

示例:

# test_sample.py
import allure


# 两数相加
def add(x, y):
    return x + y

# 测试类
@allure.severity(allure.severity_level.TRIVIAL)
class TestAdd:

    @allure.severity(allure.severity_level.MINOR)
    def test_first(self):
        assert add(3, 4) == 7

    @allure.severity(allure.severity_level.NORMAL)
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.severity(allure.severity_level.CRITICAL)
    def test_three(self):
        assert add(3, -4) == -1

    @allure.severity(allure.severity_level.BLOCKER)
    def test_four(self):
        assert add(-3, -4) == -7
 

运行:

 
E:workspace-pyPytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:workspace-pyPytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================
 

报告:

Pytest自动化测试第1张

你还可以通过 --allure-severities 选项指定严重等级运行,多个以逗号分隔。

 
E:workspace-pyPytest>pytest test_sample.py --allure-severities normal,critical
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:workspace-pyPytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ..                                                                                                                                                  [100%]

=========================================================================== 2 passed in 0.02s ===========================================================================
 
 
2、功能

如果你想对测试功能、测试场景进行行为描述,可以分别使用装饰器:@allure.feature 和 @allure.story 

示例:

 
# test_sample.py
import allure

# 两数相加
def add(x, y):
    return x + y

@allure.feature('测试类')
class TestAdd:

    @allure.story('01测试两个正数相加')
    def test_first(self):
        assert add(3, 4) == 7

    @allure.story('02测试负数正数相加')
    def test_second(self):
        assert add(-3, 4) == 1

    @allure.story('03测试正数负数相加')
    def test_three(self):
        assert add(3, -4) == -1

    @allure.story('04测试两个负数相加')
    def test_four(self):
        assert add(-3, -4) == -7
 

运行:

 
E:workspace-pyPytest>pytest test_sample.py --alluredir=report --clean-alluredir
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:workspace-pyPytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py ....                                                                                                                                                [100%]

=========================================================================== 4 passed in 0.06s ===========================================================================
 

报告:

Pytest自动化测试第2张

你也可以通过 --allure-features 和 --allure-stories 选择指定具体功能和故事运行,多个以逗号分隔。

 
E:workspace-pyPytest>pytest test_sample.py --allure-stories 01测试两个正数相加
========================================================================== test session starts ==========================================================================
platform win32 -- Python 3.7.3, pytest-6.0.2, py-1.9.0, pluggy-0.13.0
rootdir: E:workspace-pyPytest
plugins: allure-pytest-2.8.18, assume-2.3.3, cov-2.10.1, html-3.0.0, rerunfailures-9.1.1, xdist-2.1.0
collected 4 items                                                                                                                                                        

test_sample.py .                                                                                                                                                   [100%]

=========================================================================== 1 passed in 0.02s ===========================================================================
 
 

3、步骤

如果你想对每个测试调用进行非常详细的逐步说明,可以通过 @allure.step 装饰器来实现(固件同样支持)。

该装饰器会将方法或函数的调用与提供的参数一起添加到报表中,并且可以包含一条描述行,该行支持位置关键字参数。

示例:

 
# test_sample.py
import pytest
import allure

@allure.step('两数相加:{0} + {y}')
def add(x, y):
    r = x + y
    print_res(r)
    return r

@allure.step
def print_res(r):
    print('计算结果:', r)

class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]
 

报告:

Pytest自动化测试第3张

 
4、标题

如果你想让测试标题更具可读性,可以使用 @allure.title 装饰器,该装饰器支持参数的占位符并支持动态替换。

示例:

 
# test_sample.py
import pytest
import allure
def add(x, y):
    return x + y
class TestLearning:
    data = [
        [3, 4, 7],
        [-3, 4, 1],
        [3, -4, -1],
        [-3, -4, -7],
    ]
    @allure.title("测试用例-{data}")
    @pytest.mark.parametrize("data", data)
    def test_add(self, data):
        assert add(data[0], data[1]) == data[2]
 
报告:
Pytest自动化测试第4张
 
5、描述

如果你想添加测试的详细说明,可以通过添加测试方法描述信息,也可以使用装饰器 @allure.description 和 @allure.description_html 

示例:

 
# test_sample.py
import allure

# 被测功能
def add(x, y):
    return x + y

# 测试类
class TestLearning:

    @allure.description('测试正数相加')
    def test_first(self):
        assert add(3, 4) == 7

    @allure.description_html('<h1> 测试负数相加 </h1>')
    def test_second(self):
        """你也可以在这里添加用例的描述信息,但是会被allure装饰器覆盖"""
        assert add(-3, -4) == -7
 

报告:

Pytest自动化测试第5张

 
6、附件

如果你想在报告中显示不同类型的附件,可以通过以下两种方式来实现:
allure.attach(body, name, attachment_type, extension)
allure.attach.file(source, name, attachment_type, extension)

示例:

import allure


def test_multiple_attachments():
    allure.attach.file(r'C:UsersPublicPicturesSample PicturesKoala.jpg', attachment_type=allure.attachment_type.JPG)
    allure.attach('<head></head><body> 测试页面 </body>', 'Attach with HTML type', allure.attachment_type.HTML)

报告:

Pytest自动化测试第6张

 

7、链接

如果你想关联缺陷追踪系统或测试管理系统,你可以使用装饰器 @allure.link@allure.issue@allure.testcase

示例:

 
import allure

@allure.link('http://www.baidu.com')
def test_with_named_link():
    pass

@allure.issue('101', '缺陷问题描述')
def test_with_issue_link():
    pass

@allure.testcase('http://this.testcase.com', '测试用例标题')
def test_with_testcase_link():
    pass
 

报告:

Pytest自动化测试第7张

注意:

@allure.issue 将提供带有小错误图标的链接,该描述符将测试用例ID作为输入参数,以将其与提供的问题链接类型的链接模板一起使用。
链接模板在 --allure-link-patternPytest 的配置选项中指定。链接模板和类型必须使用冒号指定:
pytest test_sample.py --alluredir=report --allure-link-pattern=issue:http://www.mytesttracker.com/issue/{}

免责声明:文章转载自《Pytest自动化测试》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇在.xml文件中模糊查询的常用的3种方法DAC8562/8563深入研究学习笔记,方便V7用户手册使用,更新完毕(2020-03-27)下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

数据挖掘实践(19):算法基础(二)Logistic回归(逻辑斯蒂)算法

1 基本函数确立 1.1 Sigmoid函数:变量以及自变量 而当我们考虑二值化问题时,由于目标变量只能取0或1,因此我们选择了值域在{0,1}区间的Sigmoid函数 Sigmoid函数又叫做Logistic函数,或者Logistic Sigmoid函数,也被经常称作S型曲线    1.2 提问  1.3 代码实验 import numpy as n...

Debug与Release版本的区别

  Debug 和 Release 并没有本质的区别,他们只是VC预定义提供的两组编译选项的集合,编译器只是按照预定的选项行动。如果我们愿意,我们完全可以把Debug和Release的行为完全颠倒过来。当然也可以提供其他的模式,例如自己定义一组编译选项,然后命名为MY_ABC等。习惯上,我们仍然更愿意使用VC已经定义好的名称。    Debug版本包括调试...

使用Android Studio时so文件打包不到APK中

1,需要在build中添加如下配置,这是必备的 Android {   sourceSets {       main {           jniLibs.srcDirs = ['libs']       }   }} 2,如果添加了该项配置出现了问题可能原因是引用了第三方包引起 比如我这边引用了xutils这个包,因为该包中也有一个.so的文件lib...

[参考资料] 80个Python经典资料(教程+源码+工具)汇总

AD : 2018重磅地面课程《机器读心术之语音识别前沿实战特训营》,迈向人工智能新高度 【专题推荐】Python系列英文原版电子书 http://down.51cto.com/zt/104 python简明教程(CHM) http://down.51cto.com/data/49213 Linux黑客的python编程之道【pdf】推荐 http...

sklearn中的数据集的划分

sklearn数据集划分方法有如下方法: KFold,GroupKFold,StratifiedKFold,LeaveOneGroupOut,LeavePGroupsOut,LeaveOneOut,LeavePOut,ShuffleSplit,GroupShuffleSplit,StratifiedShuffleSplit,PredefinedSplit,...

SQL Server 2008 删除大量数据

一、写在前面 - 想说爱你不容易   为了升级数据库至SQL Server 2008 R2,拿了一台现有的PC做测试,数据库从正式库Restore(3个数据库大小夸张地达到100G+),而机器内存只有可怜的4G,不仅要承担DB Server角色,同时也要作为Web Server,可想而知这台机器的命运是及其惨烈的,只要MS SQL Server一启动,内...