The answer as stated by Tomasz is correct. However, it can become tedious to ensure that the imports in __init__.py
match your file structure.
To automatically detect all tests in the folder you can add this in __init__.py
:
import unittest
def suite():
return unittest.TestLoader().discover("appname.tests", pattern="*.py")
This will allow you to run ./manage.py test appname
but won't handle running specific tests. To do that you can use this code (also in __init__.py
):
import pkgutil
import unittest
for loader, module_name, is_pkg in pkgutil.walk_packages(__path__):
module = loader.find_module(module_name).load_module(module_name)
for name in dir(module):
obj = getattr(module, name)
if isinstance(obj, type) and issubclass(obj, unittest.case.TestCase):
exec ('%s = obj' % obj.__name__)
Now you can run all your tests via manage.py test app
or specific ones via manage.py test app.TestApples