实验:Unittest核心API(二)
作者:reader1   类别:Java开发    日期:2019-01-15 14:20:48    阅读:523 次   消耗积分:0 分

注:本节教材内容,与上期推送的《教材连载:Unittest核心API(一)》为一个章节。

回顾上节内容,请点击:教材连载:Unittest核心API(一)


2.断言


TestCase类中提供了丰富的断言去检查和报告错误,下面的表格中列出了最常用的一些方法。第一列代表使用方式,第二列代表检查项,第三列代表开始引入的版本号。前面的例子已经介绍过assertEqual(a,b)的用法,作用是对比参数a和b,相等则通过,反之测试不通过。


Method

Checks that

New in

assertEqual(a, b)

a == b


assertNotEqual(a, b)

a != b


assertTrue(x)

bool(x) is True


assertFalse(x)

bool(x) is False


assertIs(a, b)

a is b

3.1

assertIsNot(a, b)

a is not b

3.1

assertIsNone(x)

x is None

3.1

assertIsNotNone(x)

x is not None

3.1

assertIn(a, b)

a in b

3.1

assertNotIn(a, b)

a not in b

3.1

assertIsInstance(a, b)

isinstance(a, b)

3.2

assertNotIsInstance(a, b)

not isinstance(a, b)

3.2


下面展示了assertTrue方法的源码,可以看到有两个参数,expr为表达式,当表达式结果为True时,测试通过,反之不通过,第二个参数msg为断言失败后用户自定义输出的信息,默认值为None,即默认无输出。


    def assertTrue(self, expr, msg=None):

        """Check that the expression is true."""

        if not expr:

            msg = self._formatMessage(msg, "%s is not true" % safe_repr(expr))

            raise self.failureException(msg)


版权所有,转载本站文章请注明出处:蜗牛笔记, http://www.woniunote.com/article/83
上一篇:实验:Unittest核心API(一)
下一篇:Unittest高级应用
${comment['nickname']}   ${comment['createtime']}
  
       
${comment.content}
${reply.nickname} 回复 ${comment.nickname}    ${reply.createtime}
     
  
回复内容:${reply.content}