Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
636 views
in Technique[技术] by (71.8m points)

unit testing - how to add dozen of test cases to a test suite automatically in python

i have dozen of test cases in different folders. In the root directory there is a test runner.

unittest
  package1
    test1.py
    test2.py
  package2
    test3.py
    test4.py
  testrunner.py

Currently I added the four test cases manually into a test suite

import unittest
from package1.test1 import Test1
from package1.test2 import Test2
from package2.test3 import Test3
from package2.test4 import Test4

suite = unittest.TestSuite()
suite.addTests(unittest.makeSuite(Test1))
suite.addTests(unittest.makeSuite(Test2))
suite.addTests(unittest.makeSuite(Test3))
suite.addTests(unittest.makeSuite(Test4))

result = unittest.TextTestRunner(verbosity=2).run(suite)
if not result.wasSuccessful():
  sys.exit(1)

How to let test runner test all test cases automatically? Such as:

for testCase in findTestCases():
  suite.addTests(testCase)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In my opinion you should switch to unittest2 or other test frameworks with discovery features. Discovery tests is a really sane way to run them.

Most known are:

For example with nosetest is sufficient to run nosetests from the project root directory and it will discover and run all the unit tests it find. Pretty simple.

Notice also that unittest2 will be included in python 2.7 (and backported till 2.4 I guess).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...