• pytest测试框架 -- skip跳过执行测试用例


      跳过执行测试用例

    1、@pytest.mark.skip(reason=" ") -- 跳过执行测试函数

    可传入一个非必须参数reason表示原因

    import pytest
    @pytest.mark.skip(reason="no reason") def test_01(): print("---用例a执行---") class TestCase(): @pytest.mark.skip(reason="no reason") def test_02(self): print("---用例b执行---") def test_03(self): print("---用例c执行---")

    输出结果:
    test_fixture2.py ss---用例c执行---

    2、@pytest.mark.skipif(condition...) -- 若满足condition,则跳过测试函数

    传入condition参数为判断条件,可以选择传入非必须参数reason;如果多个标签一起使用,满足其中一个跳过条件则会跳过该测试函数。

    import pytest
    def
    test_01(): print("---用例a执行---") class TestCase(): #当多个@pytest.mark.skipif()标签时,若满足一个,则跳过测试函数 @pytest.mark.skipif(condition='a' >= 'b', reason="no reason") @pytest.mark.skipif(condition='a' <= 'b', reason="no reason") def test_02(self): print("---用例b执行---") def test_03(self): print("---用例c执行---")

    输出结果:
    test_fixture2.py ---用例a执行---
    .s---用例c执行---

    3、自定义@pytest.mark.skip()标签

    myskip = pytest.mark.skip() 或 myskip = pytest.mark.skipif(condition=...)

    装饰时用该变量代替标签即可:@myskip

    import pytest
    # myskip = pytest.mark.skip()
    myskip = pytest.mark.skipif(condition=2>1, reason="no reason") @myskip def test_01(): print("---用例a执行---") class TestCase(): @myskip def test_02(self): print("---用例b执行---") def test_03(self): print("---用例c执行---")

    输出结果:
    test_fixture2.py ss---用例c执行---

    4、通过pytest.skip()方法跳过测试函数

    import pytest

    def
    test_01(): pytest.skip(msg="no reason") print("---用例a执行---") class TestCase(): def test_02(self): pytest.skip() print("---用例b执行---") def test_03(self): print("---用例c执行---")

    输出结果:
    test_fixture2.py ss---用例c执行--

    5、跳过测试类

    跳过测试类其实和跳过测试方法一样,使用@pytest.mark.skip()和@pytest.mark.skipif()两个标签,用他们装饰测试类就好啦。

    import pytest

    myskip = pytest.mark.skip(reason="no reason") def test_01(): print("---用例a执行---") @myskip class TestCase(): def test_02(self): print("---用例b执行---") def test_03(self): print("---用例c执行---")

    输出结果:
    test_fixture2.py ---用例a执行---

    6、跳过模块

    使用pytestmark(不可更改变量名)变量,让他等于标签即可。

    import pytest
    
    pytestmark = pytest.mark.skip(condition=2>1, reason='no reason')
    
    def test_01():
        print("---用例a执行---")
    
    class TestCase():
    
        def test_02(self):
            print("---用例b执行---")
    
        def test_03(self):
            print("---用例c执行---")

    输出结果:
    test_fixture2.py sss

    7、pycharm中运行多个测试文件

    依次将要运行的文件名写在后面即可,用逗号隔开,无需链表元组等形式。

    if __name__ == "__main__":
        pytest.main(['-s', 'test_fixture1.py', 'test_fixture2.py'])

    参考:https://blog.csdn.net/qq_39721240/article/details/88726606

  • 相关阅读:
    开发常见错误之 :Missing artifact com.sun:tools:jar 1.7.0
    开发常见错误之 : Detected both log4j-over-slf4j.jar AND slf4j-log4j12.jar
    kafka集群部署
    kafka
    Oracle 学习之触发器
    CloudSetuper
    erlang :打开界面工具的命令
    erlang 二进制中 拼接 变量或者函数 报错
    Python内部机制。
    AOP (面向切面编程)
  • 原文地址:https://www.cnblogs.com/Maruying/p/13397426.html
Copyright © 2020-2023  润新知