I have a weird problem at hand.
I’m creating a django package and would like to provide some test cases for it.
The package is named ajax_forms and the full path is part of my PYTHONPATH.
This directory also contains a tests directory (with the __init__.py file) a tests.py file (containing the tests) and a urls.py file.
Now I want to run the tests.
The ajax_forms parent directory has an example project setup with ajax_forms in INSTALLED_APPS.
But I can’t seem to let the tests run:
>> ./manage.py test ajax_forms
Creating test database for alias 'default'...
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
No tests are found so I try to be more specific:
>> ./manage.py test ajax_forms.tests
....
raise ValueError("Test label '%s' does not refer to a test" % label)
ValueError: Test label 'ajax_forms.tests' does not refer to a test
(ajax-validation)
Even ./manage.py ajax_forms.tests.tests.AjaxFormViewTest will not find the tests and gives the same error as above.
But trying to import the test case from the ./manage.py shell works fine
In [2]: from ajax_forms.tests.tests import AjaxFormViewTest
I’m kinda clueless here, on what’s going on.
You need to put
from tests import *in your__init__.pyfileDjango will look in your app for a package/module called tests, and then interrogate its namespace for test cases. In this case it will look at the namespace of your tests package (from the init.py file), and discover nothing there. By doing the import in the
__init__.pyfile then you will import the testcases from your tests module into the namespace of the tests package, and django will be able to locate them and run them.