概述
利用python进行测试时,测试用例的加载方式有2种:
一种是通过unittest.main()来启动所需测试的测试模块,上篇文章就是使用的这种方式;
一种是添加到testsuite集合中再加载所有的被测试对象,而testsuit里存放的就是所需测试的用例,下面具体介绍这种方式。
通过suit.addTest()/suit.addTests()加载用例到suit
suit.addTest()用于添加单个测试类或者测试类中的单个方法,suit.addTests()用于添加一个列表。
案例1:通过class("test_xxx")来加载测试用例,如下:
1 # coding:utf-8 2 3 import unittest 4 5 #被测试方法 6 def cal(a,b): 7 return a+b 8 9 class singletest(unittest.TestCase): 10 11 def setUp(self): 12 print "setUp" 13 pass 14 15 def tearDown(self): 16 print "tearDown" 17 pass 18 19 def test_01(self): 20 print ("test_01") 21 expected = 6 22 result = cal(2, 4) 23 self.assertEqual(expected, result) 24 25 def test_02(self): 26 print ("test_02") 27 expected = 3 28 result = cal(2, 1) 29 self.assertEqual(expected, result) 30 31 if __name__ == '__main__': 32 suite = unittest.TestSuite() 33 suite.addTest(singletest("test_01")) 34 suite.addTest(singletest("test_02")) 35 runner = unittest.TextTestRunner() 36 runner.run(suite)
运行结果如下:
1 setUp 2 test_01 3 tearDown 4 setUp 5 test_02 6 tearDown 7 .. 8 ---------------------------------------------------------------------- 9 Ran 2 tests in 0.000 10 11 OK 12 13 Process finished with exit code 0
全部运行通过!
案例2:将class("test_xxx")格式的方法定义为列表,如下:
1 # coding:utf-8 2 3 import time,random 4 import HTMLTestRunner 5 import unittest 6 import os 7 8 #被测试方法 9 def cal(a,b): 10 return a+b 11 12 class singletest(unittest.TestCase): 13 14 def setUp(self): 15 print "setUp" 16 pass 17 18 def tearDown(self): 19 print "tearDown" 20 pass 21 22 def test_01(self): 23 print ("test_01") 24 expected = 6 25 result = cal(2, 4) 26 self.assertEqual(expected, result) 27 28 def test_02(self): 29 print ("test_02") 30 expected = 3 31 result = cal(2, 1) 32 self.assertEqual(expected, result) 33 34 if __name__ == '__main__': 35 suite = unittest.TestSuite() 36 tests = [singletest("test_01"),singletest("test_02")] 37 suite.addTests(tests) 38 runner = unittest.TextTestRunner() 39 runner.run(suite)
运行结果如下:
1 setUp 2 test_01 3 tearDown 4 setUp 5 test_02 6 tearDown 7 .. 8 ---------------------------------------------------------------------- 9 Ran 2 tests in 0.000s 10 11 OK 12 13 Process finished with exit code 0
案例3:通过makeSuite方式加载测试用例,如下:
1 # coding:utf-8 2 3 import time,random 4 import HTMLTestRunner 5 import unittest 6 import os 7 8 #被测试方法 9 def cal(a,b): 10 return a+b 11 12 class singletest(unittest.TestCase): 13 14 def setUp(self): 15 print "setUp" 16 pass 17 18 def tearDown(self): 19 print "tearDown" 20 pass 21 22 def test_01(self): 23 print ("test_01") 24 expected = 6 25 result = cal(2, 4) 26 self.assertEqual(expected, result) 27 28 def test_02(self): 29 print ("test_02") 30 expected = 3 31 result = cal(2, 1) 32 self.assertEqual(expected, result) 33 34 if __name__ == '__main__': 35 suite = unittest.TestSuite() 36 suite.addTest(unittest.makeSuite(singletest)) 37 runner = unittest.TextTestRunner() 38 runner.run(suite)
运行结果如下:
1 setUp
2 test_01
3 tearDown
4 setUp
5 test_02
6 tearDown
7 ..
8 ----------------------------------------------------------------------
9 Ran 2 tests in 0.000s
10
11 OK
12
13 Process finished with exit code 0
案例4:通过TestLoader().loadTestsFromTestCase()方式加载测试用例,如下:
1 # coding:utf-8 2 3 import unittest 4 5 #被测试方法 6 def cal(a,b): 7 return a+b 8 9 class TestCase1(unittest.TestCase): 10 11 def setUp(self): 12 print "setUp" 13 pass 14 15 def tearDown(self): 16 print "tearDown" 17 pass 18 19 def test_01(self): 20 print ("test_01") 21 expected = 6 22 result = cal(2, 4) 23 self.assertEqual(expected, result) 24 25 def test_02(self): 26 print ("test_02") 27 expected = 3 28 result = cal(2, 1) 29 self.assertEqual(expected, result) 30 31 class TestCase2(unittest.TestCase): 32 33 def setUp(self): 34 print "setUp" 35 pass 36 37 def tearDown(self): 38 print "tearDown" 39 pass 40 41 def test_03(self): 42 print ("test_03") 43 expected = 8 44 result = cal(3, 5) 45 self.assertEqual(expected, result) 46 47 def test_04(self): 48 print ("test_04") 49 expected = 10 50 result = cal(5, 5) 51 self.assertEqual(expected, result) 52 53 if __name__ == '__main__': 54 suite1 = unittest.TestLoader().loadTestsFromTestCase(TestCase1) 55 suite2 = unittest.TestLoader().loadTestsFromTestCase(TestCase2) 56 suite = unittest.TestSuite([suite1, suite2]) 57 unittest.TextTestRunner().run(suite)
运行结果如下:
1 .... 2 ---------------------------------------------------------------------- 3 Ran 4 tests in 0.000s 4 5 OK 6 setUp 7 test_01 8 tearDown 9 setUp 10 test_02 11 tearDown 12 setUp 13 test_03 14 tearDown 15 setUp 16 test_04 17 tearDown 18 19 Process finished with exit code 0
全部运行通过!
案例5:通过TestLoader().loadTestsFromName()方式加载测试用例,如下:
1 # coding:utf-8 2 3 import time,random 4 import HTMLTestRunner 5 import unittest 6 import os 7 8 #被测试方法 9 def cal(a,b): 10 return a+b 11 12 class singletest(unittest.TestCase): 13 14 def setUp(self): 15 print "setUp" 16 pass 17 18 def tearDown(self): 19 print "tearDown" 20 pass 21 22 def test_01(self): 23 print ("test_01") 24 expected = 6 25 result = cal(2, 4) 26 self.assertEqual(expected, result) 27 28 def test_02(self): 29 print ("test_02") 30 expected = 3 31 result = cal(2, 1) 32 self.assertEqual(expected, result) 33 34 if __name__ == '__main__': 35 suite = unittest.TestLoader().loadTestsFromName('testcase.singletest')#文件名为testcase 36 runner = unittest.TextTestRunner() 37 runner.run(suite)
运行结果如下:
1 .. 2 ---------------------------------------------------------------------- 3 setUp 4 test_01 5 tearDown 6 setUp 7 test_02 8 tearDown 9 Ran 2 tests in 0.000s 10 11 OK 12 13 Process finished with exit code 0
案例6:通过TestLoader().loadTestsFromNames()方式加载测试用例,如下:
1 # coding:utf-8 2 3 import time,random 4 import HTMLTestRunner 5 import unittest 6 import os 7 8 #被测试方法 9 def cal(a,b): 10 return a+b 11 12 class singletest(unittest.TestCase): 13 14 def setUp(self): 15 print "setUp" 16 pass 17 18 def tearDown(self): 19 print "tearDown" 20 pass 21 22 def test_01(self): 23 print ("test_01") 24 expected = 6 25 result = cal(2, 4) 26 self.assertEqual(expected, result) 27 28 def test_02(self): 29 print ("test_02") 30 expected = 3 31 result = cal(2, 1) 32 self.assertEqual(expected, result) 33 34 if __name__ == '__main__': 35 suite = unittest.TestLoader().loadTestsFromNames(['testcase.singletest'])#testcase为当前文件名 36 runner = unittest.TextTestRunner() 37 runner.run(suite)
运行结果如下:
1 ..
2 ----------------------------------------------------------------------
3 setUp
4 test_01
5 tearDown
6 setUp
7 test_02
8 tearDown
9 Ran 2 tests in 0.000s
10
11 OK
12
13 Process finished with exit code 0
下面针对上述脚本中应用到的unittest模块下的几个成员进行简单的介绍,以便于理解上述代码:
TestCase:所有测试用例的基本类,给一个测试方法的名字,就会返回一个测试用例实例;
TestSuit:组织测试用例的实例,支持测试用例的添加和删除,最终将传递给 testRunner进行测试执行;
TextTestRunner:进行测试用例执行的实例,其中Text的意思是以文本形式显示测试结果。测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息;
TestLoader:用来加载TestCase到TestSuite中的,其中有几个 loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例。