• pytest 常用方法介绍


     前言

      之前一篇文章简单介绍了 pytest 以及 pytest.fixture 装饰器 :https://www.cnblogs.com/shenh/p/11572657.html 。实际在写自动化测试脚本中,还会有一些很实用的方法,下文就来讲述下这些用法。

    一.pytest.mark.parametrize 装饰器

     pytest 内置装饰器 @pytest.mark.parametrize 可以让测试数据参数化,把测试数据单独管理,类似 ddt 数据驱动的作用,方便代码和测试数据分离。

    1.一次传多个参数

    import pytest
    
    @pytest.mark.parametrize('x,y',[(1,2),(3,4)])
    def test_sum(x,y):
        sum = x + y
        print(sum)
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s'])

    执行结果:

    test_sample.py 
    3
    .
    7
    .
    
    ============================== 2 passed in 0.06s ==============================

    2.组合传参:

     注意:这种方式一共传递了4组参数  (1,3)、(1,4)、(2,3)、(2,4)。这种方式可以简化测试数据,不用手动再将参数组合。

    import pytest
    
    @pytest.mark.parametrize('x',[1,2])
    @pytest.mark.parametrize('y',[3,4])
    def test_sum(x,y):
        sum = x + y
        print(sum)
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s'])

     执行结果:

    test_sample.py 
    4
    .
    5
    .
    5
    .
    6
    .
    
    ============================== 4 passed in 0.14s ==============================

    二、fixture返回值

    1.获取被调用函数返回值

    import pytest
    
    @pytest.fixture(scope='function')
    def login():
        accesstoken = '197ce8083c38467f'
    
        return accesstoken
    
    
    def test_sum(login):
        token = login
        print(token)
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s'])

     执行结果:

    test_sample.py 
    197ce8083c38467f
    .
    
    ============================== 1 passed in 0.04s ==============================

     若被调用函数返回多个参数:

    import pytest
    
    @pytest.fixture(scope='function')
    def login():
        accesstoken = '197ce8083c38467f'
        customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
    
        return accesstoken,customerguid
    
    
    def test_sum(login):
        token = login[0]
        guid = login[1]
        print(token)
        print(guid)
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s'])

     执行结果:

    test_sample.py 
    197ce8083c38467f
    096799f5-e040-11e9-8c01-0242ac11000d
    .
    
    ============================== 1 passed in 0.07s ==============================

    2.单个用例调用多个函数

    import pytest
    
    @pytest.fixture(scope='function')
    def login():
        print('登录')
    
    @pytest.fixture(scope='function')
    def conn():
        print('连接数据库')
    
    def test_1(login,conn):
        print('测试用例1')
    
    def test_2():
        print('测试用例2')
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s'])

     执行结果:

    test_sample.py 
    登录
    连接数据库
    测试用例1
    .
    测试用例2
    .
    
    ============================== 2 passed in 0.05s ==============================

    三、测试用例分类

     有时候我们只需执行部分测试用例,比如从用例集当中挑选 smoke 测试,要怎么做呢?通过装饰器 @pytest.mark.smoke,smoke 是可以自定义的,运行时加上命令‘-m=smoke’,pytest 就会挑选带有装饰器的类或函数运行。

    import pytest
    
    @pytest.fixture(scope='function')
    def login():
        accesstoken = '197ce8083c38467f'
        customerguid = '096799f5-e040-11e9-8c01-0242ac11000d'
    
        return accesstoken,customerguid
    
    @pytest.mark.smoke
    def test_sum(login):
        token = login[0]
        guid = login[1]
        print(token)
        print(guid)
    
    def test_2():
        print('测试用例')
    
    if __name__ =="__main__":
        pytest.main(['test_sample.py','-s','-m=smoke'])

     执行结果:

    test_sample.py 
    197ce8083c38467f
    096799f5-e040-11e9-8c01-0242ac11000d
    .
    
    ======================= 1 passed, 1 deselected in 0.02s =======================
  • 相关阅读:
    [No000037]操作系统Operating Systems操作系统历史与硬件概况History of OS & Summaries!
    [No000036]操作系统Operating Systems系统调用的实现System_Call
    [No000035]操作系统Operating System之OS Interface操作系统接口
    [No000034]知乎-长期接收碎片化知识有什么弊端?
    [No000033]码农网-如何锻炼出最牛程序员的编码套路
    [No000032]程序员的年龄天花板
    [No000031]操作系统 Operating Systems 之Open the OS!
    [No000030]程序员节发点别的:中国教育整个把人脑子搞坏了-易中天
    [No00002F]3步,教你如何分解需求
    [No00002E]关于大数据,你不知道的6个迷思
  • 原文地址:https://www.cnblogs.com/shenh/p/11593256.html
Copyright © 2020-2023  润新知