Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to spread django unit tests over multiple files?

  • I have a python-django application
  • I'm using the unit testing framework
  • The tests are arranged in the file "tests.py" in the module directory
  • I'm running the tests via ./manage.py test app

Now..

  • The tests.py file is getting rather large/complex/messy
  • I'd like to break tests.py up into smaller collections of tests...

How?

like image 724
John Mee Avatar asked Oct 17 '22 19:10

John Mee


1 Answers

Note that this approach is no longer valid from Django 1.6, see this post.

You can create tests folder with ___init___.py inside (so that it becomes a package). Then you add your split test .py files there and import all of them in ___init___.py.

I.e: Substitute the test.py file with a module that looks and acts like the file:

Create a tests Directory under the app in question

app
app\models.py
app\views.py
app\tests
app\tests\__init__.py
app\tests\bananas.py
app\tests\apples.py

Import the submodules into app\tests\__init__.py:

from bananas import *
from apples import *

Now you can use ./manage.py as if they were all in a single file:

./manage.py test app.some_test_in_bananas
like image 133
Tomasz Zieliński Avatar answered Oct 19 '22 07:10

Tomasz Zieliński