本文首发在我的个人博客:https://jlice.top/p/7gbvt/。欢迎大家前去参观,么么哒~
执行流程
使用要点
一个 testcase 类应该派生自 unittest.TestCase
setUpClass()
、tearDownClass()
必须使用@classmethod
装饰器
assertEqual
一般first是预期值,second是实际值。
断言抛出异常
with self.assertRaises(SomeException):
do_something()
assertTrue、assertFalse
assertTrue
实际上是断言 bool(expr) 为 True 。同理,assertFalse
实际上是断言 bool(expr) 为 False 。 因此,若要断言 expr 为 True 或 False,不要用assertTrue
或assertFalse
。
命令行执行unittest
# 运行test_module
python -m unittest test_module
# 运行TestClass
python -m unittest test_module.TestClass
# 运行test_method
python -m unittest test_module.TestClass.test_method