第四章:检查测试用例
1、用例测试断言
在测试用例的执行过程中,往往需要设置一些检查点,用于判断用例执行是否符合预期。下面将介绍如何使用断言。
假设我们需要测试一个字符串拼接的函数:
def string_combine(a,b):
return a+b
测试用例的代码如下:
from testbase.testcase import TestCase
class StrCombineTest(TestCase):
'''测试字符串拼接接口
'''
owner = "foo"
status = TestCase.EnumStatus.Ready
priority = TestCase.EnumPriority.Normal
timeout = 1
def run_test(self):
#---------------------------
self.start_step("测试字符串拼接")
#---------------------------
result = string_combine("xxX", "yy")
self.assert_("检查string_combine调用结果", result == "xxXyy")
以上的代码执行结果如下:
============================================================
测试用例:StrCombineTest 所有者:foo 优先级:Normal 超时:1分钟
============================================================
----------------------------------------
步骤1: 测试字符串拼接
============================================================
测试用例开始时间: 2016-02-02 14:10:21
测试用例结束时间: 2016-02-02 14:10:21
测试用例执行时间: 00:00:0.00
测试用例步骤结果: 1:通过
测试用例最终结果: 通过
============================================================
可以看到结果是测试通过的,但是如果string_combine实现有问题,比如我们新定义一个string_combine:
def string_combine(a,b):
return a +'b'
因为以上的实现是有问题,执行结果必然是不通过的:
===========================