PDF版 ePub版

# 单元测试

# -*- coding: utf-8 -*-

import unittest

class TestStringMethods(unittest.TestCase):

def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO')    # 判断两个值是否相等

def test_isupper(self):
self.assertTrue('FOO'.isupper())          # 判断值是否为 True
self.assertFalse('Foo'.isupper())         # 判断值是否为 False

def test_split(self):
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):         # 检测异常
s.split(2)

• assertEqual 用于判断两个值是否相等；
• assertTrue/assertFalse 用于判断表达式的值是 True 还是 False；
• assertRaises 用于检测异常；

• 检测两个值的大小关系：相等，大于，小于等
• 检查逻辑表达式的值：True/Flase
• 检查异常

Method Checks that
assertEqual(a, b) a == b
assertNotEqual(a, b) a != b
assertGreater(a, b) a > b
assertGreaterEqual(a, b) a >= b
assertLess(a, b) a < b
assertLessEqual(a, b) a <= b
assertTrue(x) bool(x) is True
assertFalse(x) bool(x) is False
assertIs(a, b) a is b
assertIsNot(a, b) a is not b
assertIsNone(x) x is None
assertIsNotNone(x) x is not None
assertIn(a, b) a in b
assertNotIn(a, b) a not in b
assertIsInstance(a, b) isinstance(a, b)
assertNotIsInstance(a, b) not isinstance(a, b)

$python -m unittest mytest test_isupper (mytest.TestStringMethods) ... ok test_split (mytest.TestStringMethods) ... ok test_upper (mytest.TestStringMethods) ... ok 执行结果： ... ---------------------------------------------------------------------- Ran 3 tests in 0.000s OK 上面的结果表明测试通过，我们也可以加 -v 参数得到更加详细的测试结果：$ python -m unittest -v mytest
test_isupper (mytest.TestStringMethods) ... ok
test_split (mytest.TestStringMethods) ... ok
test_upper (mytest.TestStringMethods) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

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

$python mytest.py # setUp 和 tearDown 在某些情况下，我们需要在每个测试方法执行前和执行后做一些相同的操作，比如我们想在每个测试方法执行前连接数据库，执行后断开数据库连接，为了避免在每个测试方法中编写同样的代码，我们可以使用 setUp 和 tearDown 方法，比如： # -*- coding: utf-8 -*- import unittest class TestStringMethods(unittest.TestCase): def setUp(self): # 在每个测试方法执行前被调用 print 'setUp, Hello' def tearDown(self): # 在每个测试方法执行后被调用 print 'tearDown, Bye!' def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') # 判断两个值是否相等 def test_isupper(self): self.assertTrue('FOO'.isupper()) # 判断值是否为 True self.assertFalse('Foo'.isupper()) # 判断值是否为 False def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello', 'world']) # check that s.split fails when the separator is not a string with self.assertRaises(TypeError): # 检测异常 s.split(2) 看看执行结果：$ python -m unittest -v mytest
test_isupper (mytest.TestStringMethods) ... setUp, Hello
tearDown, Bye!
ok
test_split (mytest.TestStringMethods) ... setUp, Hello
tearDown, Bye!
ok
test_upper (mytest.TestStringMethods) ... setUp, Hello
tearDown, Bye!
ok

----------------------------------------------------------------------
Ran 3 tests in 0.000s

OK

# 小结

• 通过从 unittest.TestCase 继承来编写测试类。
• 使用断言方法判断程序运行的结果是否和预期相符。
• setUp 在每个测试方法执行前被调用，tearDown 在每个测试方法执行后被调用。