2

I was having a problem that the django test runner wasn't finding the tests for my app, like this one:

Django test runner not finding tests

One of the comments on that thread suggested creating a new app with django-admin.py and seeing if the tests ran there. e.g.

django-admin.py startapp delme

then

adding "delme" to my INSTALLED_APPS

then

copying my tests.py from the app where it wasn't getting found into delme/

and viola! the tests did run. So, OK, I have a work arround, but I don't understand. I re-read what I think should be the relevant parts of the django documentation but the penny refuses to drop.

BTW, the app works via runserver and wsgi, so there doesn't appear to be any gross configuration problem. And my tests all pass from their new home, so I obviously need more tests :)

Specifically, I'm running django in a virtualenv, so I had run "django-admin.py startapp" in the (activated) virtualenv where I wanted the tests to run. This doesn't make the tests run in my other virtualenvs, I still have the old symptoms there (Ran 0 tests). I have a multitude of virtualenvs, managed by non-trivial paver scripts. One uses "path.copytree" for deploying projects, rewrites apache config files, restarts apache, writes wsgi files using the appropriate virtualenv, etc. The other uses PIP/GCC/aptitude/etc for bootstrapping/tearing down the different environments, updating packages as per configuration, etc. So I want to understand the difference between django-startapp and simply copying files, so I can fix these paver scripts so the tests can run in any environment I want them to.

Community
  • 1
  • 1

2 Answers2

0

The only thing that makes sense to me, after reading your description, is the location of paths for your existing apps. Can you confirm the following things:

  1. Your app is at the same folder level as the delme app
  2. Your app folder contains an __init__.py file
  3. Your app is listed in the INSTALLED_APPS setting

I'm going to guess that it's a missing __init__.py file, as that trips some people up. To answer your specific question, django-admin startapp doesn't do anything magical. It just creates the right folders and files in the correct place.

Your folder structure should be...

my_project/
    __init__.py
    manage.py
    settings.py
    my_app/
        __init__.py
        tests.py
        models.py
    delme/
        __init__.py
        tests.py
        models.py

Also note this comment

You can't easily name the TestCase class directly. You name the app, the Django runner does it's discovery thing by looking in models.py and tests.py

Community
  • 1
  • 1
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • Yes on all 3 counts. My non-testing app is adjacent to the new "delme" app, it has an (empty) `__init__.py`, and its listed in INSTALLED_APPS. When run from delme/, my tests hit the non-testing app (and pass). – Chris Gough Apr 11 '12 at 07:49
  • Also, it doesn't make any difference if I run the tests as on the whole project with `manage.py test --settings=mysettings` or on just the app with `manage.py test delme --settings=mysettings` (works), `manage.py test non-testing --settings=mysettings` (doesn't work). – Chris Gough Apr 11 '12 at 07:57
  • If what you say is true, and django-admin does no magic (only create files/directories), then I must be mangling my path or something weird like that. I'll check that out tomorrow when the day starts. – Chris Gough Apr 11 '12 at 11:39
0

solved (still with a little whiff of magic).

diff showed that my old app didn't have a models.py but the new app ("delme", working) did. I didn't think the old app needed one, it was importing all it's domain classes from other places.

Touching an empty models.py in my old app fixed it, now the test runner finds the tests.py and everything works as expected. Condlusion - if an app has no models.py, the django test runner won't find the app's tests.py.

What I said about not working in different virtualenvs was bogus (red herring), I was a bit confused about what my deploy scripts were doing.