unittest学习5-断言

unittest提供了以下断言方式:

方法

检查

新进

assertEqual(a, b)

== b

 

assertNotEqual(a, b)

!= b

 

assertTrue(x)

bool(x) is True

 

assertFalse(x)

bool(x) is False

 

assertIs(a, b)

is b

3.1

assertIsNot(a, b)

is not b

3.1

assertIsNone(x)

is None

3.1

assertIsNotNone(x)

is not None

3.1

assertIn(a, b)

in b

3.1

assertNotIn(a, b)

not in b

3.1

assertIsInstance(a, b)

isinstance(a, b)

3.2

assertNotIsInstance(a, b)

not isinstance(a, b)

3.2

所有assert方法都接受一个msg参数,该参数可以作为错误消息输出。例如:

 

 assertEqual(firstsecondmsg=None)

  比较first和second是否相等,不相等可以指定输出msg,测试失败

assertNotEqual(firstsecondmsg=None)

  比较first和second是否不相等,如果值相等,则输出失败

assertTrue(exprmsg=None)

 assertFalse(expr,msg=None)

  测试expr为true或者false

assertIs(firstsecondmsg=None)

assertIsNot(firstsecondmsg=None)

  判断first和second是否为同一对象

assertIsNone(exprmsg=None)

assertIsNotNone(exprmsg=None

  判断expr是否为None

assertIn(firstsecondmsg=None)

assertNotIn(firstsecondmsg=None)

  判断first是否在second中

assertIsInstance(objclsmsg=None)

assertNotIsInstance(objclsmsg=None)

  判断obj是否为cls的实例

 

posted @ 2019-10-30 16:28  测试爬虫  阅读(139)  评论(0编辑  收藏  举报