单个Module的测试,相对较为简单,只需要引入unittest,创建自己的测试类,并继承unittest.testcase ,并执行unittest.main()即可自动执行所有以test开头的测试代码,可以使用与gtest类似的断言
import unittest
class MyClass():
def __init__(self):
pass
def sqare(self, x):
return x*x
class MyTest(unittest.TestCase):
def test1(self):
self.failUnless(MyClass().sqare(3) == 9)
if __name__ == '__main__':
unittest.main()
执行结果:
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK