【02_Unittest跳过测试和预期失败】

一、 跳过测试

Unittest 支持跳过单个或整组的测试用例,跳过测试的方式:

  1. 通过条件判断跳过单个测试用例执行,如:@unittest.skipIf(3>2,"条件成立跳过")
  2. 通过调用 skipTest() 跳过单个测试用例执行
  3. 通过 @unittest.skip() 装饰器跳过整组的测试用例

1.通过条件判断跳过单个测试用例执行

test_case_skip_01.py

import unittest
import sys
# 通过skip装饰器中的条件判断是否跳过用例的用法
class TestCaseSkip01(unittest.TestCase):

    @unittest.skip("无条件跳过")
    def test_01(self):
        print("test_01")

    @unittest.skipIf(3>2,"条件成立跳过")
    def test_02(self):
        print("test_02")

    @unittest.skipUnless(1<0,"跳过这个用例,除非条件为真才执行")
    def test_03(self):
        print("test_03")

    @unittest.skipUnless(sys.platform.startswith("win"), "requires Windows")
    def test_windows_support(self):
        print("在windows上才会执行")

# 执行结果:
# Skipped: 无条件跳过
# Skipped: 条件成立跳过
# Skipped: 跳过这个用例,除非条件为真才执行
# 在windows上才会执行

2.通过调用 skipTest() 跳过单个测试用例执行

test_case_skip_02.py


import unittest
# 通过条件判断,直接调用skipTest()跳过执行
class TestCaseSkip02(unittest.TestCase):

    def external_resource_available(self):
        """"伪代码:外部资源是否可用的判断方法"""
        return False  # 表示外部资源不可用

    def test_maybe_skipped(self):
        if self.external_resource_available() == False:
            self.skipTest("external resource not available")  #调用skipTest()跳过执行
        # test code that depends on the external resource
        print("test_maybe_skipped") # if条件成立,用例会被跳过,此行不会被执行,
if __name__ == '__main__':
    unittest.main()

# 执行结果:
# OK (skipped=1)

3. 通过 @unittest.skip() 装饰器跳过整组的测试用例

test_case_skip_03.py

import unittest
'''
使用@unittest.skip()装饰器,跳过整个测试类
整个类中的测试用例都不会执行
'''
@unittest.skip("showing class skipping")
class MySkippedTestCase(unittest.TestCase):
    def test_not_run1(self):
        print("test_not_run1")
    def test_not_run2(self):
        print("test_not_run2")

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

# 执行结果
# Ran 2 tests in 0.004s
# OK (skipped=2)
# Process finished with exit code 0
# Skipped: showing class skipping
# Skipped: showing class skipping

二、预期失败

多用于编写异常场景的测试用例,举个例子
正常场景:用户名正确、密码正确 —> 登录响应—>断言code=200
异常场景:用户名正确、密码传空 —> 登录响应—> 断言code= 200 断言是失败的,但测试验证功能、场景是正常的
示例:

expectedFailure.py

import unittest
import random

class ExpectedFailureTestCase(unittest.TestCase):

    def get_login_code(self,name,password):
        """伪代码,调用login接口获取响应"""
        if (name == "admin" and password == "admin112SS"):
            return 200
        else:
            response_code_list = [400,401,402,403,404]
            return response_code_list[random.randint(0,5)]

    def test_login_success(self):
        """正常场景验证"""
        actual_code = self.get_login_code("admin","admin112SS")
        self.assertEqual(actual_code, 200, "登录成功")

    @unittest.expectedFailure
    def test_login_fail(self):
        """异常场景验证"""
        actual_code = self.get_login_code("admin"," ")
        # print("actual_code=",actual_code)
        self.assertEqual(actual_code, 200, "登录参数错误")  #参数不正确,那么code!=200  但是具体code是多少不一定就是一个固定的404

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

# 执行主要结果:
# Ran 1 test in 0.004s
# 
# OK (expected failures=1)
# ....
#     raise self.failureException(msg)
# AssertionError: 402 != 200 : 登录参数错误
  • test_login_fail() 使用@unittest.expectedFailure 执行的日志: OK (expected failures=1) 代表测试场景通过,如果是接口测试,说明接口正常
  • test_login_fail() 未使用@unittest.expectedFailure 执行的日志:FAILED (failures=1) 测试场景没通过,如果是接口测试,说明接口存在bug

常见的unittest中测试用例执行结果和对应含义

        1、FAILED(errors=1) 可能是本身python编码错误
        2、FAILED (failures=1) 断言没通过,所测的接口或功能异常
        3、OK 断言成功
        4、OK(expected failures = 1) 断言通过,预期失败,所测接口、功能正常
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值