5

i tried:

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

but i get eror:

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'

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

error: Traceback (most recent call last): File "runtests.py", line 64, in ? results = main() File "runtests.py", line 58, in main results = unittest.TextTestRunner().run(buildTestSuite()) File "runtests.py", line 52, in buildTestSuite module =__import__(modname, fromlist='1') TypeError: __import__() takes no keyword arguments
__import__ accepts keyword arguments in Python2.6+ (at least). What version of Python are you using?
I see. Instead of using sys.modules, I think module=__import__(modname,{},{},['1']) should work also.
0

Try something like:

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))

1 Comment

i get error: Traceback (most recent call last): File "runtests.py", line 76, in ? results = main() File "runtests.py", line 70, in main results = unittest.TextTestRunner().run(buildTestSuite()) File "/usr/lib64/python2.4/unittest.py", line 696, in run test(result) TypeError: 'NoneType' object is not callable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.