利用unittest测试框架可以编写测试用例,执行方式分两大类:利用main方法和利用testsuite,其中利用测试套件来组织测试用例可以有4种写法。
在此之前,先了解几个概念
- TestCase:所有测试用例的基本类,给一个测试方法的名字,就会返回一个测试用例实例;
- TestSuite:组织测试用例的实例,支持测试用例的添加和删除,最终将传递给 testRunner进行测试执行;
- TextTestRunner:进行测试用例执行的实例,其中Text的意思是以文本形式显示测试结果。测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息;
- TestLoader:用来加载TestCase到TestSuite中的,其中有几个 loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例;
- defaultTestLoader.discover:将discover方法筛选出来的用例,循环添加到测试套件中,打印出的用例信息会递增
方法一:通过unittest.main()来执行测试用例的方式
import unittest class TestStringMethods(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello','world']) with self.assertRaises(TypeError): s.split(2) if __name__ == "__main__": #import sys;sys.argv = ['', 'Test.testName'] unittest.main()
unittest.main()会去查找所有以test开头的方法,并把它们当做一个个测试用例
方法二:用测试套件
import unittest class TestStringMethods(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello','world']) with self.assertRaises(TypeError): s.split(2) def suite(): suite = unittest.TestSuite() suite.addTest(TestStringMethods('test_upper')) suite.addTest(TestStringMethods('test_isupper')) unittest.TextTestRunner(verbosity=2).run(suite) if __name__ == "__main__": suite() #import sys;sys.argv = ['', 'Test.testName'] # unittest.main()
方法三:用TestLoader
import unittest class TestCase1(unittest.TestCase): #def setUp(self): #def tearDown(self): def testCase1(self): print 'aaa' def testCase2(self): print 'bbb' class TestCase2(unittest.TestCase): #def setUp(self): #def tearDown(self): def testCase1(self): print 'aaa1' def testCase2(self): print 'bbb1' if __name__ == "__main__": #此用法可以同时测试多个类 suite1 = unittest.TestLoader().loadTestsFromTestCase(TestCase1) suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCase2) suite = unittest.TestSuite([suite1, suite2]) unittest.TextTestRunner(verbosity=2).run(suite)
方法四:测试套件的一种变性,和方法二差不多
import unittest class TestStringMethods(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello','world']) with self.assertRaises(TypeError): s.split(2) def suite(): tests = ['test_upper','test_isupper'] suite = unittest.TestSuite(map(TestStringMethods,tests)) unittest.TextTestRunner(verbosity=2).run(suite) if __name__ == "__main__": suite() #import sys;sys.argv = ['', 'Test.testName'] # unittest.main()
方法五:用discover
import unittest class TestStringMethods(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) def tearDown(self): unittest.TestCase.tearDown(self) def test_upper(self): self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self): self.assertTrue('FOO'.isupper()) self.assertFalse('Foo'.isupper()) def test_split(self): s = 'hello world' self.assertEqual(s.split(), ['hello','world']) with self.assertRaises(TypeError): s.split(2) def suite(): suite=unittest.TestSuite() discover=unittest.defaultTestLoader.discover(casepath, pattern='test_*.py', top_level_dir=None) #将discover方法筛选出来的用例,循环添加到测试套件中,打印出的用例信息会递增 for test_case in discover: suite.addTests(test_case) print(suite) return suite if __name__ == "__main__": all_test_cases = suite() unittest.TextTestRunner().run(all_test_cases) #import sys;sys.argv = ['', 'Test.testName'] # unittest.main()