Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing only project in Django

When I try to run python manage.py test in my Django (1.4) project, I get the error:

ERROR: test_site_profile_not_available (django.contrib.auth.tests.models.ProfileTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/slacy/src/tmp/env/local/lib/python2.7/site-packages/django/contrib/auth/tests/models.py", line 29, in test_site_profile_not_available
    del settings.AUTH_PROFILE_MODULE
  File "/home/slacy/src/tmp/env/local/lib/python2.7/site-packages/django/utils/functional.py", line 215, in __delattr__
    delattr(self._wrapped, name)
AttributeError: AUTH_PROFILE_MODULE

This is documented in a Django bug, with a recomendation to only test specific apps instead of all. However my project has no apps, the models.py simply resides in the project root. To test a specific app in Django, it looks like this:

$ ./manage.py test animals
Note that we used animals, not myproject.animals.

meaning that it is not possible to specify the root directory to test. How would I test just my project directory?

Note that this bug is part of a larger discussion on unit testing discovery.

like image 451
saul.shanabrook Avatar asked May 16 '12 02:05

saul.shanabrook


People also ask

How do I test my Django project?

The preferred way to write tests in Django is using the unittest module built-in to the Python standard library. This is covered in detail in the Writing and running tests document. You can also use any other Python test framework; Django provides an API and tools for that kind of integration.

Is Django used for testing?

Django provides a test framework with a small hierarchy of classes that build on the Python standard unittest library. Despite the name, this test framework is suitable for both unit and integration tests. The Django framework adds API methods and tools to help test web and Django-specific behavior.

How do I run a specific app in Django?

It is possible to run a single app's tests standalone, without creating a Django test project for that purpose. One way of doing so is by creating a runtests.py in your app's root dir which setups Django settings and runs ./manage.py test your_app programmatically.


1 Answers

I would recommend using django-discover-runner. It allows you to specify and full dotted path to test.

If you just run ./manage.py test, it'll discover and run all tests underneath the TEST_DISCOVER_ROOT setting (a file system path). If you run ./manage.py test full.dotted.path.to.test_module, it'll run the tests in that module (you can also pass multiple modules). If you give it a single dotted path to a package (like a Django app) like ./manage.py test myapp and that package does not itself directly contain any tests, it'll do test discovery in all submodules of that package.

like image 137
saul.shanabrook Avatar answered Sep 22 '22 17:09

saul.shanabrook