Django uses python's unittest framework. Digging through django's code, I see that It basically imports the app.tests module and runs the unittests loadTestsFromModule. Inspecting this code shows us,
def loadTestsFromModule(self, module):As you can see it looks for any names the module has defined. So I can put my tests in the test module and in __init__.py do import file_name.class_name to make django find my tests
"""Return a suite of all tests cases contained in the given module"""
tests = []
for name in dir(module):
obj = getattr(module, name)
if (isinstance(obj, (type, types.ClassType)) and
issubclass(obj, TestCase)):
tests.append(self.loadTestsFromTestCase(obj))
return self.suiteClass(tests)
2 comments:
Hi,
Thanks for the tip. I am having trouble using it though.
"As you can see it looks for any names the module has defined. So I can put my tests in the test module and in __init__.py do import file_name.class_name to make django find my tests "
Let's say I have following structure:
myproject/myapplication/models.py
myproject/myapplication/views.py
myproject/myapplication/tests.py (tests for models and views)
myproject/mymodule/foobar.py
myproject/mymodule/tests.py (tests for foobar)
mymodule won't get tested by default because Django seeks only tests (tests.py file inside folder) of application (module which contains views and models and is mentioned on settings.py of the project).
Do you mean that __init__.py (inside myproject/myapplication) should contain "import myproject.mymodule.tests.SomeTestCase" for each test case? I suppose the idea is that the Django function will then find those test cases.
One alternative solution I thought about was to create a custom test runner and then use a decorator to alter tests fed to it. I haven't gotten this to work still. I am not sure if this is possible even.
As the last resort I might have to make a patch for Django altering the nasty little function a bit.
I think he/she (sorry) meant create a tests directory instead of a tests.py file. And within that directory, create a __init__.py file to import your tests. So in your example:
myproject/myapplication/tests/__init__.py
What I do is add all my test files in that directory, like this:
myproject/myapplication/tests/app.py
myproject/myapplication/tests/models.py
myproject/myapplication/tests/rpc.py
and in /myproject/myapplicatoin/tests/__init__.py, I have:
from app import *
from models import *
from rpc import *
hth
Post a Comment