简体   繁体   中英

How to investigate an error inside python unittest load_tests method?

Python unittest has a method load_tests , which, in theory, can be used inside a module to define the tests to be loaded. I have tried to use the 'do nothing' example literally:

def load_tests(loader, standard_tests, pattern):
    # top level directory cached on loader instance
    this_dir = os.path.dirname(__file__)
    package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
    standard_tests.addTests(package_tests)
    return standard_tests

and get an error:

ERROR: LoadTestsFailure test_example

Without specifying the load_tests in my module the test function correctly. How can I find out the problem I have?.

More information: this_dir is a valid path, loader is a <unittest.loader.TestLoader`` object and pattern` is empty.

If you are using an ide like PyDev you can add a breakpoint, or alternatively you can add a try/except block:

def load_tests(loader, standard_tests, pattern):
    # top level directory cached on loader instance
try:
    this_dir = os.path.dirname(__file__)
    package_tests = loader.discover(start_dir=this_dir, pattern=pattern)
    standard_tests.addTests(package_tests)
except Exception as e:
    print 'Error loading tests: %s' % str(e)
return standard_tests

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM