Want to have unit tests in multiple files

142 views
Skip to first unread message

Steve

unread,
May 6, 2008, 4:21:22 PM5/6/08
to Django developers, bko...@gmail.com
Hi, we're just getting started using Django unit tests, and it looks
like the documentation says you can only have unit tests in two files:
models.py and tests.py. We would prefer to put our unit tests in many
different files, with, say, each main-line .py file having a
corresponding unit-test file. Is there a convenient way to do this in
Django?

Honza Král

unread,
May 6, 2008, 4:26:48 PM5/6/08
to django-d...@googlegroups.com
Hi,
you can always define your own test runner which will look not only in
tests and models, but in every module.

This question is more suited for the django-users mailing list, this
list is intended for discussing development of django internals.

--
Honza Král
E-Mail: Honza...@gmail.com
ICQ#: 107471613
Phone: +420 606 678585

Alex Koshelev

unread,
May 6, 2008, 5:26:06 PM5/6/08
to Django developers
No. Not `tests.py`, but `tests` module - that can be a package of many
other modules/files

Gary Wilson Jr.

unread,
May 23, 2008, 2:32:45 PM5/23/08
to django-d...@googlegroups.com
Alex Koshelev wrote:
> No. Not `tests.py`, but `tests` module - that can be a package of many
> other modules/files

In case you haven't figured this out already, it can be done by
importing your unit test classes from the test/*.py modules in
tests/__init__.py

Gary

Sebastian Noack

unread,
May 23, 2008, 3:20:51 PM5/23/08
to django-d...@googlegroups.com
On Fri, 23 May 2008 13:32:45 -0500
"Gary Wilson Jr." <gary....@gmail.com> wrote:
> In case you haven't figured this out already, it can be done by
> importing your unit test classes from the test/*.py modules in
> tests/__init__.py

That is exactly what I have done at my work, just a few days ago. I
put the code below into the tests/__init__.py. You can use it as is.

def get_test_modules():
from os import path, listdir

names = set()
for f in listdir(path.dirname(__file__)):
if f.startswith('.') or f.startswith('__'):
continue
names.add(f.split('.')[0])

for name in names:
yield (name, __import__('%s.%s' % (__name__, name), {}, {}, ['']))

def setup_doc_tests():
for name, module in get_test_modules():
# Try to find an API test in the current module, if it fails continue.
try:
api_tests = module.__test__['API_TESTS']
except (AttributeError, TypeError, KeyError):
continue

# Import possible dependecies of the API test from the current module.
for k, v in module.__dict__.iteritems():
if k.startswith('__'):
continue
globals()[k] = v

# Attach the API test to the __test__ dictionary if it exists or create it.
try:
globals()['__test__'][name] = api_tests
except KeyError:
globals()['__test__'] = {name: api_tests}

def setup_unit_tests():
import unittest

for name, module in get_test_modules():
# Import each TestCase from the current module.
for k, v in module.__dict__.iteritems():
if not (isinstance(v, type) and issubclass(v, unittest.TestCase)):
continue
globals()[k] = v

setup_doc_tests()
setup_unit_tests()

signature.asc

Graham King

unread,
May 23, 2008, 5:29:12 PM5/23/08
to django-d...@googlegroups.com
That seems very complicated, All I did was put this in tests/__init__.py

from myproj.tests.model_tests import *
from myproj.tests.view_tests import *
(etc)


2008/5/23 Sebastian Noack <sebasti...@googlemail.com>:

Sebastian Noack

unread,
May 26, 2008, 5:32:58 AM5/26/08
to django-d...@googlegroups.com
On Fri, 23 May 2008 14:29:12 -0700
"Graham King" <gra...@gkgk.org> wrote:
> That seems very complicated, All I did was put this in
> tests/__init__.py
>
> from myproj.tests.model_tests import *
> from myproj.tests.view_tests import *
> (etc)

Well, you don't have to use my code. Of course you can just import each
unit test explicitly.

The reason why I have written this code, was to don't have to import
all unit tests explicit and to support doc tests as well.

Regards
Sebastian Noack

signature.asc
Reply all
Reply to author
Forward
0 new messages