Organizing Django Tests into Folders

Update: Due to some strange Django error, Django will refuse to run your app’s tests if your application doesn’t have a models.py file. You’ll need to create an empty models.pyin your app’s folder if you don’t have one

Django comes with a nice testing  rig that lets you individually test each app.  By using a combination of pyunit & Django’s testing tools, you’ll be able to whip up a testing suite for each of your site’s apps (more information here: http://docs.djangoproject.com/en/dev/topics/testing/)

However there is some strangeness as to how Django expects the tests to be organized. From the Django documentation:

For a given Django application, the test runner looks
for doctests in two places:

    * The models.py file. You can define module-level doctests and/or
      a doctest for individual models. It's common practice to put
      application-level doctests in the module docstring and model-level
      doctests in the model docstrings.
    * A file called tests.py in the application directory
      -- i.e., the directory that holds models.py. This file is a
      hook for any and all doctests you want to write that aren't
      necessarily related to models.

Which sounds simple enough, until you end up with a bloated tests.py file that covers 5 different aspects of your app.

Now wouldn’t it be great if you could split them into different files and group them into a folder?

Putting your Tests into a Folder

  1. In your app’s folder, create a new folder called tests
  2. Break up your tests.py file into “logical” (what that exactly means is up to you :P) python files. Remember to add in the import statements for each file
  3. Move your new files into the tests folder
  4. In tests, create a new file __init__.py
  5. You want to modify your __init__.py as such:
    from [Project Name].[App Name].tests.[filename] import *
    
    #starts the test suite
    __test__= {
               'mytest': [filename]
               }

    Note that the filename’s  .py extension should be excluded from [filename]!

  6. And that’s it! Just follow those basic rules to add all your new test files to __init__.py.
    You can go ahead and delete the original tests.py file now as well. Happy testing!
This entry was posted in Django, python, Tech and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

7 Comments

  1. Zo
    Posted July 29, 2010 at 2:42 pm | Permalink

    I am trying to implement this b/c it is exactly what I have been looking. Unfortunately, I get a NameError: name ‘GeneralTests’ is not defined.

    My code for __init__.py :

    from pythonproject.testproj.tests.GeneralTests import *

    __test__= {
    ‘mytest’: GeneralTests
    }

    Python 2.6
    Django 1.2.1

    Could you offer any help please?

  2. Posted July 29, 2010 at 6:16 pm | Permalink

    Hrm, make sure GeneralTests.py is in the same folder as __init__.py, and try using from GeneralTests import * instead

  3. Posted April 14, 2011 at 4:10 pm | Permalink

    Thanks very useful

  4. Adorilson
    Posted October 20, 2011 at 9:26 pm | Permalink

    And about the docstring out of models.py?

  5. Sam
    Posted January 9, 2013 at 5:38 pm | Permalink

    I know this is an old post, but how are you supposed to call the individual tests from the CL? Is there any chance this does not work in Django 1.4?

  6. Posted January 9, 2013 at 7:37 pm | Permalink

    Hi Sam,

    As far as I know there is no way to execute only one test case from an application’s tests.py. You can, however, choose to only execute tests for a specific application module in your Django project.

  7. Sam
    Posted January 9, 2013 at 7:43 pm | Permalink

    Actually I got it to work on a per-TestCase basis:

    ` ./manage.py test tool.MyTestCase`

    Otherwise, great post!

2 Trackbacks

  • By Unit Tests in Django | Pi/Pi on March 24, 2010 at 1:52 am

    [...] Pi/Pi …but that's just one! Skip to content ProjectsCSC309 A1: kääntää TranslationsJump StacksGrksm To UnicodePhotosynth of BahenCSC302 Winter ‘09CSC301 Fall ‘08Project ArgoboxAbout MeContact Me « Organizing Django Tests into Folders [...]

  • By Django Unit Tests on March 24, 2010 at 1:55 am

    [...] general, I organize my unit tests into folders (ref. Organizing Django Tests into Folders) so I can break them down into manageable [...]

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>