原文:http://www.51testing.com/html/10/448910-3648852.html
利用python进行测试时,测试用例的加载方式有2种:
一种是通过unittest.main()来启动所需测试的测试模块;import unittest class UCTestCase(unittest.TestCase): def setUp(self): #测试前需执行的操作 ..... def tearDown(self): #测试用例执行完后所需执行的操作 ..... # 测试用例1 def testCreateFolder(self): #具体的测试脚本 ...... # 测试用例2 def testDeleteFolder(self): #具体的测试脚本 ...... if __name__ == "__main__": unittest.main() |
import unittest # 执行测试的类 class UCTestCase(unittest.TestCase): def setUp(self): #测试前需执行的操作 ..... def tearDown(self): #测试用例执行完后所需执行的操作 ..... # 测试用例1 def testCreateFolder(self): #具体的测试脚本 ...... # 测试用例2 def testDeleteFolder(self): #具体的测试脚本 ...... if __name__ == "__main__": # 构造测试集 suite = unittest.TestSuite() suite.addTest(UC7TestCase("testCreateFolder")) suite.addTest(UC7TestCase("testDeleteFolder")) # 执行测试 runner = unittest.TextTestRunner() runner.run(suite) |
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) |