Several ways for python to use unittest for test case execution

Original text: http://www.51testing.com/html/10/448910-3648852.html

When testing with python , there are two ways to load test cases :

  One is to start the test module of the required test through unittest.main(); the other
  is to add it to the testsuite collection and load all the objects under test, and the testsuit stores the use cases to be tested, which are listed below. The specific usage of the 3 methods:
1. The way to execute the test case through unittest.main():

import unittest 

class UCTestCase(unittest.TestCase):
    def setUp(self): #Operations
        to be performed before testing
        .....      
    def tearDown(self): #Operations to be
        performed after the test case is executed
        .....      
    # Test case 1
    def testCreateFolder(self): #Specific
        test script
        ...      
    # Test case 2
    def testDeleteFolder(self): #Specific
        test script
        ...       
if __name__ == "__main__":
    unittest.main()


2. The way to execute test cases through testsuit:

import unittest 
# The class that executes the test
class UCTestCase(unittest.TestCase):
    def setUp(self): #The
        operation to be performed before the test
        .....       
    def tearDown(self): #The operation to be
        performed after the test case is executed
        .....
       
    # Test case 1
    def testCreateFolder(self): #Specific
        test script
        ...      
    # Test case 2
    def testDeleteFolder(self): #Specific
        test script
        ...       
if __name__ = = "__main__":
    # Construct the test
    suite suite = unittest.TestSuite()
    suite.addTest(UC7TestCase("testCreateFolder"))
    suite.addTest(UC7TestCase("testDeleteFolder")) #Execute 
    the test
    runner = unittest.TextTestRunner()
    runner.run(suite)


3. Through the testLoader method:

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)

 

  下面针对上述脚本中应用到的unittest模块下的几个成员进行简单的介绍,以便于理解上述代码:
 TestCase:所有测试用例的基本类,给一个测试方法的名字,就会返回一个测试用例实例;
 TestSuit:组织测试用例的实例,支持测试用例的添加和删除,最终将传递给  testRunner进行测试执行;
 TextTestRunner:进行测试用例执行的实例,其中Text的意思是以文本形式显示测试结果。测试的结果会保存到TextTestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息;
 TestLoader:用来加载TestCase到TestSuite中的,其中有几个  loadTestsFrom__()方法,就是从各个地方寻找TestCase,创建它们的实例,然后add到TestSuite中,再返回一个TestSuite实例;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325217109&siteId=291194637