2011-03-19 169 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)