unittest 单元测试框架断言方法

unittest单元测试框架的TestCase类下,测试结果断言方法:Assertion methods

方法 检查 版本
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

assertEqual(first, second, msg=None)
-assertNotEqual(first, second, msg=None)

断言第一个参数和第二个参数是否相等,如果不相等则测试失败。msg 为可选参数,用于定义测试失败时所打印的信息。

例:

测试代码

# coding:utf-8
from django.test import TestCase class MyTest(TestCase):
def setUp(self):
number=input("Enter a number:") #input输入信息
self.number=int(number) def test_case(self):
self.assertEqual(self.number,10,msg="Your input is not 10") 在cmd执行测试:
D:\Python27\Scripts\myweb>python manage.py test blog.test3.MyTest.test_case
Creating test database for alias 'default'...
Enter a number:12 #输入数字
F
======================================================================
FAIL: test_case (blog.test3.MyTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "D:\Python27\Scripts\myweb\blog\test3.py", line 10, in test_case
    self.assertEqual(self.number,10,msg="Your input is not 10")
AssertionError: Your input is not 10 ----------------------------------------------------------------------
Ran 1 test in 2.613s FAILED (failures=1)
Destroying test database for alias 'default'... 127
上一篇:C# 字符串详细使用


下一篇:java 线程(1)