unittest 学习 3 - 跳过测试用例

本文介绍了unittest框架中6种跳过测试用例的方法,包括直接强制跳过、给定条件成立时跳过、默认跳过、标记为预期失败/报错等。这些方法确保了当特定条件满足时,测试用例、setUp和tearDown相关方法不会被执行。
摘要由CSDN通过智能技术生成

被跳过的用例 / 类 / 模块,与之相应的 setUp() / tearDown()、setUpClass / tearDownClass()、setUpModule() / tearDownModule() 也不会被执行。

6种跳过用例的方法

  1. 直接强制跳过
# reason 为说明跳过原因
@unittest.skip(reason)

实例

import unittest


class myTestCase(unittest.TestCase):

    def test01(self):
        print('test01')

    @unittest.skip('跳过test02')
    def test02(self):
        print('test02')

    def test03(self):
        print('test03')


if __name__ == '__main__':
    unittest.main()

执行结果

Ran 3 tests in 0.048s

OK (skipped=1)

Process finished with exit code 0
test01

Skipped: 跳过test02
test03
  1. 给定条件成立时跳过
# 条件condition为True时跳过
@unittest.skipIf(conidition, reason)

实例


                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值