当前位置: 首页> 技术文章> Pytest 单元测试框架之skip跳过用例

Pytest 单元测试框架之skip跳过用例

1、Pytest 中 skip 跳过用例

  • 标记 skip 表示跳过该用例,不运行

    • pytest.mark.skip(reason="str")

# 在方法上条件装饰器 skip,跳过单条用例
import pytest
class Test01():
    def test_szdcs(self):    
        print("深圳多测师")

    @pytest.mark.skip(reason="跳过的用例")
    def test_gzdcs(self):    
        print("广州多测师")
        
class Test02():
    def test_shdcs(self):    
        print("上海多测师")
        
# 运行命令         
pytest -sv test_demo1.py

# 结果如下
test_demo1.py::Test01::test_szdcs 深圳多测师
PASSED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED
# 在类上面添加装饰器 skip,跳过整个类中的所有用例
import pytest

@pytest.mark.skip(reason="跳过的用例")
class Test01():
    def test_szdcs(self):    
        print("深圳多测师")    
    def test_gzdcs(self):    
        print("广州多测师")
        
class Test02():
    def test_shdcs(self):    
        print("上海多测师")

# 运行命令                
pytest -sv test_demo1.py

# 结果如下
test_demo1.py::Test01::test_szdcs 
SKIPPED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED

2、Pytest 中 skipif 跳过用例

  • 通过条件判断是否忽略不执行该用例

  • 判断条件表达式 pytest.mark.skipif(condition,reason="str")

import pytest
class Test01():
    def test_szdcs(self):    
        print("深圳多测师")

    @pytest.mark.skipif(2>1,reason="条件正确不执行")    
    def test_gzdcs(self):    
        print("广州多测师")class Test02():
    def test_shdcs(self):    
        print("上海多测师")
        
# 运行命令        
pytest -sv test_demo1.py

# 结果如下
test_demo1.py::Test01::test_szdcs 深圳多测师
PASSED
test_demo1.py::Test01::test_gzdcs 
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多测师
PASSED


上一篇: python中对多态的理解

下一篇: 软件测试之手工测试人员如何转测试开发?

QQ技术交流群

多测师官方学习交流
556733550

加入群聊