如何在Python中的單個測試套件中運行多個類單元測試.....如何在Python單元測試中的單一測試套件中運行多個類?
12
A
回答
23
如果要從特定的測試類列表中運行所有測試,而不是模塊中所有測試類的所有測試,可以使用TestLoader
的loadTestsFromTestCase
方法獲得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
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)
相關問題
- 1. Python單元測試:在另一個模塊中運行測試
- 2. 對多個文件運行Python單元測試測試
- 3. Python中的單元測試套件
- 4. 如何在多個測試類中運行一組junit測試?
- 5. 問題的運行單元測試測試套件OO
- 6. 我如何在Python創建一個測試套件單元測試
- 7. 在unittest python中運行單個測試
- 8. Python套件測試不運行測試
- 9. 在單元測試子類NOT中運行單一測試功能
- 10. 單元測試組織 - 如何不運行基類測試?
- 11. 在LSF上運行python單元測試
- 12. 嵌套函數運行單元測試
- 13. Python中的單元測試
- 14. Maven Surefire:運行一個單元測試
- 15. 在Visual Studio中執行單個Boost測試單元測試
- 16. 從MbUnit中的許多夾具運行單個單元測試
- 17. Python類和單元測試
- 18. MSBuild運行單元測試
- 19. Phabricator:運行單元測試
- 20. SuperNotCalledException運行單元測試
- 21. Python單元測試沒有運行
- 22. 單元測試DAL - 停止運行單元測試時連接
- 23. 是否可以從Scala的測試套件中運行單個測試?
- 24. Python單元測試:我如何測試異常中的參數?
- 25. 在maven中運行單個測試 - >沒有執行測試!
- 26. 未運行類庫(UWP)單元測試
- 27. 如何運行單元測試
- 28. 如何在python單元測試中測試python二進制文件?
- 29. 角單元測試與多個expectGET在一個單一的測試
- 30. 我可以在套件中運行單個測試嗎?
你的意思是? http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory – amdstorm 2012-07-16 12:42:58
您的意思是? http://stackoverflow.com/questions/1732438/run-all-unit-test-in-python-directory它在一個目錄中運行多個python測試類 – amdstorm 2012-07-16 12:44:01