Testing only project in Django
Asked Answered
F

2

6

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.

Fortress answered 16/5, 2012 at 2:34 Comment(0)
F
4

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.

Fortress answered 20/5, 2012 at 13:25 Comment(1)
Starting on Django 1.6, there is a new test discover runner which does exactly what django-discover-runner does for 1.5 and below.Solomonsolon
P
1

Create a new directory called apps, move your entire application into it minus the settings.py, manage.py, urls.py and whatever else is added by default by django-admin.py startproject and then run

./manage.py test apps

In the future, avoid keeping your entire application in one directory. Separate it into sub-apps based on functionality, for this very reason

Pier answered 16/5, 2012 at 4:26 Comment(3)
But my project only has four models, and they all seem rather related to me. I didn't think it was wrong to have only a project with no apps in it, as long as it didn't get too large.Fortress
Until another solution comes around, I guess I will just make one app and put everything in there.Fortress
I separated my project and realized that in fact it was helpful.Fortress

© 2022 - 2024 — McMap. All rights reserved.