pytest(二)跳过测试用例及setup与teardown

1.跳过测试用例:使用跳过装饰器,我们可以跳过指定的测试
skip的用法
@pytest.mark.skip(reason=“不想执行的缘由,执行时会输出reason内容”)
#skipif的用法
@pytest.mark.skipif(条件表达式, reason=“”)
当条件表达式返回True时,会跳过用例
(一)函数级别跳过
在这里插入图片描述
(二)类级别跳过,跳过类中skip和skipif的用法一样,唯一的就是将skip或者skipif的装饰器放在要跳过的类上面,详见如下:

@pytest.mark.skip('跳过类TestDemo2')
class TestDemo2():
    def test_pytest_5(self):
        print('case5')
    def test_pytest_6(self):
        print('case6')

(三)模块级别跳过
使用pytestmark=pytest.mark.skip()方法:可以跳过整个模块,注意pytestmark为关键字,必须用此名称

import pytest
pytestmark = pytest.mark.skip(reason='跳过test_10skipmodule02.py模块')
def test_pytest_5():
    print('case5')
def test_pytest_6():
    print('case6')
class TestDemo3():
    def test_pytest_7(self):
        print('case7')

    def test_pytest_8(self):
        print('case8')
if __name__ == '__main__':
    pytest.main(['-rs', 'test_skipmodule02.py'])

2.pytest的setup和teardown
setup和teardown主要分为:模块级,类级,功能级,函数级。
存在于测试类内部
代码示例:
1.函数级别setup()/teardown(),运行于测试方法的始末,即:运行一次测试函数会运行一次setup和teardown

	def setup(self):
        print("这是一个setup")
    def teardown(self):
        print("这是一个teardown")
    @pytest.mark.skip('跳过')
    def test_a(self):
        print('test_a')
        assert 2 == 2
    def test_b(self):
        print('test_b')
        assert 3 == 3
    def test_c(self):
        print('test_c')
        assert 5 == 5
2.类级别,运行于测试类的始末,即:在一个测试内只运行一次setup_class和teardown_class,不关心测试类内有多少个测试函数
class TestDemo:
    def setup_class(self):
        print("这是一个setup_class")
    def teardown_class(self):
        print("这是一个teardown_class") 
@pytest.mark.skip('跳过')
 def test_a(self):
     print('test_a')
     assert 2 == 2
 def test_b(self):
     print('test_b')
     assert 3 == 3
 def test_c(self):
     print('test_c')
     assert 5 == 5
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值