2011-03-19 167 views
12

如何在Python中的單個測試套件中運行多個類單元測試.....如何在Python單元測試中的單一測試套件中運行多個類?

+0

你的意思是? http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory – amdstorm 2012-07-16 12:42:58

+0

您的意思是? http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory它在一個目錄中運行多個python測試類 – amdstorm 2012-07-16 12:44:01

回答

23

如果要從特定的測試類列表中運行所有測試,而不是模塊中所有測試類的所有測試,可以使用TestLoaderloadTestsFromTestCase方法獲得TestSuite測試爲每個類,然後創建一個單一TestSuite結合套房的列表,您可以用run使用:

import unittest 

# Some tests 

class TestClassA(unittest.TestCase): 
    def testOne(self): 
     # test code 
     pass 

class TestClassB(unittest.TestCase): 
    def testOne(self): 
     # test code 
     pass 

class TestClassC(unittest.TestCase): 
    def testOne(self): 
     # test code 
     pass 

if __name__ == '__main__': 
    # Run only the tests in the specified classes 

    test_classes_to_run = [TestClassA, TestClassC] 

    loader = unittest.TestLoader() 

    suites_list = [] 
    for test_class in test_classes_to_run: 
     suite = loader.loadTestsFromTestCase(test_class) 
     suites_list.append(suite) 

    big_suite = unittest.TestSuite(suites_list) 

    runner = unittest.TextTestRunner() 
    results = runner.run(big_suite) 

    # ... 
0

我發現nose是一個很好的工具。它發現目錄結構中的所有單元測試並執行它們。

+0

可以u爲這個問題發佈解決方案,請... – passionTime 2011-03-22 05:02:02

12

我對你在這裏提出的問題有點不確定,但是如果你想知道如何在同一套件中測試多個類,通常你只需要在同一個python文件中創建多個測試類並將它們一起運行:

import unittest 

class TestSomeClass(unittest.TestCase): 
    def testStuff(self): 
      # your testcode here 
      pass 

class TestSomeOtherClass(unittest.TestCase): 
    def testOtherStuff(self): 
      # testcode of second class here 
      pass 

if __name__ == '__main__': 
    unittest.main() 

而且隨着例如運行:

python mytestsuite.py 

更好的例子可以在the official documention找到。

如果另一方面要運行多個測試文件,如"How to organize python test in a way that I can run all tests in a single command?"中所述,那麼其他答案可能會更好。

0

通常你會用下列方式(其每間套房僅增加了一類):

# Add tests. 
alltests = unittest.TestSuite() 
alltests.addTest(unittest.makeSuite(Test1)) 
alltests.addTest(unittest.makeSuite(Test2)) 

如果您想擁有每間套房多個類,您可以使用下面的方法添加這些測試:

for name in testnames: 
    suite.addTest(tc_class(name, cargs=args)) 

這裏是相同的例子運行每單獨套件中的所有類,你可以定義自己的make_suite方法:

# Credits: http://codereview.stackexchange.com/a/88662/15346 
def make_suite(tc_class): 
    testloader = unittest.TestLoader() 
    testnames = testloader.getTestCaseNames(tc_class) 
    suite = unittest.TestSuite() 
    for name in testnames: 
     suite.addTest(tc_class(name, cargs=args)) 
    return suite 

# Add all tests. 
alltests = unittest.TestSuite() 
for name, obj in inspect.getmembers(sys.modules[__name__]): 
    if inspect.isclass(obj) and name.startswith("FooTest"): 
     alltests.addTest(make_suite(obj)) 

result = unittest.TextTestRunner(verbosity=2).run(alltests) 

如果上面沒有套件,可以將上面的例子轉換成可以接受多個類的方法。

1

unittest.TestLoader.loadTestsFromModule()方法將發現並加載指定模塊中的所有類。所以你可以這樣做:

import unittest 
import sys 

class T1(unittest.TestCase): 
    def test_A(self): 
    pass 
    def test_B(self): 
    pass 

class T2(unittest.TestCase): 
    def test_A(self): 
    pass 
    def test_B(self): 
    pass 

if __name__ == "__main__": 
    suite = unittest.TestLoader().loadTestsFromModule(sys.modules[__name__]) 
    unittest.TextTestRunner(verbosity=3).run(suite)