I am looking for a unit testing framework for Python. I am aware of
nose, but was wondering if there are any others that will
automatically find and run all tests under a directory hierarchy.
Have you already looked at the unittest module? Below is the code I
use for one of my current projects to load all test cases in package.
This code is sitting in __init__.py, and the test cases are in
separate files (util.py, util_threading.py, etc.). Those files can
contain as many TestCase classes as needed, all are loaded with
loadTestsFromModule. You could easily modify this code to
automatically generate the modules list if you want to.
# repo/pypaq/test/__init__.py
from unittest import TestSuite, defaultTestLoader
import logging
import sys
__all__ = ['all_tests']
modules = ['util', 'util_buffer', 'util_event', 'util_threading']
if not __debug__:
raise RuntimeError('test suite must be executed in debug mode')
all_tests = []
for name in modules:
module = __import__('pypaq.test', globals(), locals(), [name], 0)
tests = defaultTestLoader.loadTestsFromModule(getattr(module, name))
__all__.append(name)
all_tests.append(tests)
setattr(sys.modules[__name__], name, tests)
logging.getLogger().setLevel(logging.INFO)
all_tests = TestSuite(all_tests)
I then have test_pypaq.py file under repo/, with which I can execute
all_tests or only the tests from a specific module:
# repo/test_pypaq.py
from unittest import TextTestRunner
from pypaq.test import *
TextTestRunner(verbosity=2).run(all_tests)
- Max