zoukankan      html  css  js  c++  java
  • pytest--高级用法

    七、高级用法
    1、跳过测试函数
    根据特定的条件,不执行标识的测试函数.
    方法:skipif(condition, reason=None)
    参数:condition:跳过的条件,必传参数,reason:标注原因,必传参数
    使用方法:@pytest.mark.skipif(condition, reason="xxx")
    代码示例:
    import pytest
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")
        def test_a(self):
            print("------->test_a")
            assert 1
        @pytest.mark.skipif(condition=2>1,reason = "跳过该函数")   #跳过测试函数test_b
        def test_b(self):
            print("------->test_b")
                assert 0

    执行结果:

    test_abc.py 
       ------->setup_class
       ------->test_a #只执行了函数test_a
       .
       ------->teardown_class
       s # 跳过函数```
    2、标记为预期失败函数
    标记测试函数为失败函数
    方法:xfail(condition=None, reason=None, raises=None, run=True, strict=False)
    常用参数:condition:预期失败的条件,必传参数,reason:失败的原因,必传参数
    使用方法:@pytest.mark.xfail(condition, reason="xx")
    代码示例:
    import pytest
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")
        def test_a(self):
            print("------->test_a")
            assert 1
        @pytest.mark.xfail(2 > 1, reason="标注为预期失败") # 标记为预期失败函数test_b
           def test_b(self):
               print("------->test_b")
              assert 0
    执行结果:
    test_abc.py 
        ------->setup_class
        ------->test_a
        .
        ------->test_b
        ------->teardown_class
        x  # 失败标记
    3、函数数据参数化
    方便测试函数对测试输入的获取。
    方法:parametrize(argnames, argvalues, indirect=False, ids=None, scope=None)
    常用参数:
    argnames:参数名
    argvalues:参数对应值,类型必须为list
    当参数为一个时格式:[value]
    当参数个数大于一个时,格式为:[(param_value1,param_value2.....),(param_value1,param_value2.....)]
    使用方法: @pytest.mark.parametrize(argnames,argvalues) 参数值为N个,测试方法就会运行N次
    代码示例-1:
    import pytest
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
            print("------->teardown_class")
    
        @pytest.mark.parametrize("a,b",[(1,2),(0,3)]) # 参数a,b均被赋予两个值,函数会运行两遍
        def test_a(self,a,b):  # 参数必须和parametrize里面的参数一致
            print("test data:a=%d,b=%d"%(a,b))
            assert a+b == 3

    执行结果:

    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 运行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 运行第二次取值 a=0,b=3
    .
    ------->teardown_class
    代码示例-2:
    import pytest
    def return_test_data():
        return [(1,2),(0,3)]
    class Test_ABC:
        def setup_class(self):
            print("------->setup_class")
        def teardown_class(self):
                print("------->teardown_class")
    
        @pytest.mark.parametrize("a,b",return_test_data()) # 使用函数返回值的形式传入参数值
        def test_a(self,a,b):
            print("test data:a=%d,b=%d"%(a,b))
            assert a+b == 3

    执行结果:

    test_abc.py 
    ------->setup_class
    test data:a=1,b=2 # 运行第一次取值 a=1,b=2
    .
    test data:a=0,b=3 # 运行第二次取值 a=0,b=3
    .
    ------->teardown_class
    4、修改 Python traceback 输出
    pytest --showlocals    # show local variables in tracebacks
    pytest -l    # show local variables (shortcut)
    pytest --tb=auto    # (default) 'long' tracebacks for the first and last entry, but 'short' style for the other entries
    pytest --tb=long   # exhaustive, informative traceback formatting
    pytest --tb=short   # shorter traceback format
    pytest --tb=line     # only one line per failure
    pytest --tb=native   # Python standard library formatting
    pytest --tb=no   # no traceback at all
    --full-trace 参数会打印更多的错误输出信息,比参数 --tb=long 还多,即使是 Ctrl+C 触发的错误,也会打印出
     
    5、获取用例执行性能数据
    获取最慢的10个用例的执行耗时
    pytest --durations=10
     
    6.测试用例分类
    有时候我们只需执行部分测试用例,比如冒烟测试,可以先在pytest.ini中注册,再通过装饰器 @pytest.mark.smoke(smoke是可以自定义的)。
    ......
    @pytest.mark.smoke
    def test_a():
         print("测试用例a")
    
    ......

    运行时加上命令‘-m=smoke’,pytest 就会挑选带有装饰器的类或函数运行。

  • 相关阅读:
    sqlserver 获取系统用户表结构信息
    Android访问WebService的两种方法
    C# WebService的简单和复杂参数类型和结果的JSON格式
    JQuery的Ajax使用Get,Post方法调用C#WebService并返回数据
    jQuery提交Json数据到Webservice,并接收返回的Json数据
    C#开发的WebService使用JSON格式传递数据+Ajax测试
    Web项目的三层架构和MVC架构异同
    SqlServer更新视图存储过程函数脚本
    SqlServer刷新所有视图
    SqlServer获取表结构语句
  • 原文地址:https://www.cnblogs.com/absoluteli/p/13984841.html
Copyright © 2011-2022 走看看