, __all__, , :
import os.path, pkgutil, importlib
__path = os.path.dirname(__file__)
__all__ = [name for _, name, _ in pkgutil.iter_modules([__path])]
for __module in __all__:
importlib.import_module("." + __module, __name__)
Python 2.7+ code is due importlib, but it can be modified for older versions of Python using bare __import__.
Then use it as follows:
import tests
tests.test_002.test_002_func()
or
from tests import *
test_002.test_002_func()
However , if it is for unit testing, I highly recommend unittesttaking a look at or something like Nose , which will handle test detection in a more pythonic way.
source
share