我试过了:
def buildTestSuite():
suite = unittest.TestSuite()
for testcase in glob.glob('src/testsuite/test_*.py'):
module = os.path.splitext(testcase)[0]
print module
print type(module)
suite.addTest(__import__(module).buildTestSuite())
return suite
但我收到错误:
Traceback (most recent call last):
File "runtests.py", line 63, in ?
results = main()
File "runtests.py", line 57, in main
results = unittest.TextTestRunner().run(buildTestSuite())
File "runtests.py", line 53, in buildTestSuite
suite.addTest(__import__(module).buildTestSuite())
AttributeError: 'module' object has no attribute 'buildTestSuite'
答案 0 :(得分:5)
def buildTestSuite():
suite = unittest.TestSuite()
for testcase in glob.glob('src/testsuite/test_*.py'):
modname = os.path.splitext(testcase)[0]
module=__import__(modname,{},{},['1'])
suite.addTest(unittest.TestLoader().loadTestsFromModule(module))
return suite
答案 1 :(得分:0)
尝试类似:
suite = unittest.TestSuite()
for t in glob.glob('src/testsuite/test_*.py'):
try:
# If the module defines a suite() function, call it to get the suite.
mod = __import__(t, globals(), locals(), ['suite'])
suitefn = getattr(mod, 'suite')
suite.addTest(suitefn())
except (ImportError, AttributeError):
# else, just load all the test cases from the module.
suite.addTest(unittest.defaultTestLoader.loadTestsFromName(t))