unittest 使用例子
test.py
import unittest class Test1(unittest.TestCase): @classmethod def setUpClass(self): print("execute setUpClass1") @classmethod def tearDownClass(self): print("execute tearDownClass1") def setUp(self): print("execute setUp2") def tearDown(self): print("execute tearDown2") def test_one(self): print('execute test_one') self.assertTrue('FOO'.isupper()) def test_two(self): print('execute test_two') if __name__ == '__main__': unittest.main()
运行
macname@MacdeMacBook-Pro Desktop % python3 test.py execute setUpClass1 execute setUp2 execute test_one execute tearDown2 .execute setUp2 execute test_two execute tearDown2 .execute tearDownClass1 ---------------------------------------------------------------------- Ran 2 tests in 0.000s OK macname@MacdeMacBook-Pro Desktop %