Splitting tests.py

159 views
Skip to first unread message

Olivier Guilyardi

unread,
Jan 18, 2010, 2:40:16 PM1/18/10
to django...@googlegroups.com
Hi,

I'm trying to split tests.py into individual files into a tests/ subfolder, but
that doesn't work.

I correctly import everything from within tests/__init__.py, as previously said
on this mailing list:
http://groups.google.com/group/django-users/browse_frm/thread/10cfd65e398360d2/dae3a7226dccdc5f

But that doesn't work. I tested on Django 1.0.2 and SVN r12255, same thing.

Any clue?

--
Olivier

Shawn Milochik

unread,
Jan 18, 2010, 2:59:06 PM1/18/10
to django...@googlegroups.com
Here's a fantastic resource. It's what I used to switch to this methodology. It talks you through exactly how to do this.

http://ericholscher.com/blog/2008/nov/4/introduction-pythondjango-testing-basic-unit-tests/

Shawn

Reinout van Rees

unread,
Jan 18, 2010, 3:19:14 PM1/18/10
to django...@googlegroups.com

Wild guess: place the test code in normal python test files inside the
tests/ folder instead of in the __init__.py? (I tend to keep that one
virtually empty anyway).

Again: pretty wild guess ;-)

Reinout

--
Reinout van Rees - rei...@vanrees.org - http://reinout.vanrees.org
Programmer/advisor at http://www.nelen-schuurmans.nl
"Military engineers build missiles. Civil engineers build targets"

Olivier Guilyardi

unread,
Jan 18, 2010, 3:48:20 PM1/18/10
to django...@googlegroups.com
On 01/18/2010 09:19 PM, Reinout van Rees wrote:
> On 01/18/2010 08:40 PM, Olivier Guilyardi wrote:
>> Hi,
>>
>> I'm trying to split tests.py into individual files into a tests/
>> subfolder, but
>> that doesn't work.
>>
>> I correctly import everything from within tests/__init__.py, as
>> previously said
>> on this mailing list:
>> http://groups.google.com/group/django-users/browse_frm/thread/10cfd65e398360d2/dae3a7226dccdc5f
>>
>>
>> But that doesn't work. I tested on Django 1.0.2 and SVN r12255, same
>> thing.
>
> Wild guess: place the test code in normal python test files inside the
> tests/ folder instead of in the __init__.py? (I tend to keep that one
> virtually empty anyway).

Yes, I like it almost empty too :)

> Again: pretty wild guess ;-)

My __init__.py just contains an import statement, that is:

from individual_test import *

where individual_test.py resides in tests/

It doesn't work so far.

I'm not sure why, but splitting individual files tend to be a complex matter in
my Django experience. For example, when moving models from models.py to models/,
I needed to add an app_label to models' Meta class. That's a different problem,
but there seem to be some obscurity in the way things get loaded.

--
Olivier

Olivier Guilyardi

unread,
Jan 18, 2010, 3:50:02 PM1/18/10
to django...@googlegroups.com
On 01/18/2010 08:59 PM, Shawn Milochik wrote:
> Here's a fantastic resource. It's what I used to switch to this methodology. It talks you through exactly how to do this.
>
> http://ericholscher.com/blog/2008/nov/4/introduction-pythondjango-testing-basic-unit-tests/

Oh yes, I found this one.

The following doesn't work in my case (quoted from the page you cite):

"Unit tests are a lot easier to import than doctests. You simply do a from
<filename> import <testnames>. I named my unit test file unittst.py, and python
will import that from the current directory. You are importing the test classes
that you defined in your file. So I could have as easily put from unittest
import TestBasic and it would work. Using the import * syntax allows us to add
more tests to the file later and not have to edit it. "

--
Olivier


Shawn Milochik

unread,
Jan 18, 2010, 3:58:52 PM1/18/10
to django...@googlegroups.com

Then what does your tests/__init__.py look like?

Mine looks like this:

from address_tests import *
from random_tests import *
from other_tests import *

#where the tests folder contains address_tests.py, random_tests.py, and other_tests.py.

Shawn

Ramiro Morales

unread,
Jan 18, 2010, 4:04:34 PM1/18/10
to django...@googlegroups.com

What do you mean with "doesn't work" ?. Do you get a traceback? No
error but your
tests aren't being run?, ...

Try with SVN r12254 because the test infrastructure was refactored in r12255
and it still has some small rough edges.

--
Ramiro Morales | http://rmorales.net

Olivier Guilyardi

unread,
Jan 18, 2010, 4:12:20 PM1/18/10
to django...@googlegroups.com
On 01/18/2010 09:58 PM, Shawn Milochik wrote:
> On Jan 18, 2010, at 3:50 PM, Olivier Guilyardi wrote:
>
>> On 01/18/2010 08:59 PM, Shawn Milochik wrote:
>>> Here's a fantastic resource. It's what I used to switch to this methodology. It talks you through exactly how to do this.
>>>
>>> http://ericholscher.com/blog/2008/nov/4/introduction-pythondjango-testing-basic-unit-tests/
>> Oh yes, I found this one.
>>
>> The following doesn't work in my case (quoted from the page you cite):
>>
>> "Unit tests are a lot easier to import than doctests. You simply do a from
>> <filename> import <testnames>. I named my unit test file unittst.py, and python
>> will import that from the current directory. You are importing the test classes
>> that you defined in your file. So I could have as easily put from unittest
>> import TestBasic and it would work. Using the import * syntax allows us to add
>> more tests to the file later and not have to edit it. "
>>
>
> Then what does your tests/__init__.py look like?
>
> Mine looks like this:
>
> from address_tests import *
> from random_tests import *
> from other_tests import *
>
> #where the tests folder contains address_tests.py, random_tests.py, and other_tests.py.

$ cat tests/__init__.py
from model_tests import *

$ find tests
tests
tests/model_tests.py
tests/__init__.py

Django doesn't see my test cases unless I move tests/model_tests.py to tests.py.

One thing though: the tests directory (or tests.py if I revert it) is not at the
root of a project, it's located in an application that I'm developing.

--
Olivier

Olivier Guilyardi

unread,
Jan 18, 2010, 4:21:09 PM1/18/10
to django...@googlegroups.com
On 01/18/2010 10:04 PM, Ramiro Morales wrote:
> On Mon, Jan 18, 2010 at 4:40 PM, Olivier Guilyardi <m...@xung.org> wrote:
>> Hi,
>>
>> I'm trying to split tests.py into individual files into a tests/ subfolder, but
>> that doesn't work.
>>
>> I correctly import everything from within tests/__init__.py, as previously said
>> on this mailing list:
>> http://groups.google.com/group/django-users/browse_frm/thread/10cfd65e398360d2/dae3a7226dccdc5f
>>
>> But that doesn't work. I tested on Django 1.0.2 and SVN r12255, same thing.
>>
>> Any clue?
>
> What do you mean with "doesn't work" ?. Do you get a traceback? No
> error but your
> tests aren't being run?, ...

No error, the tests are not run. When splitting, it says:

Ran 0 tests in 0.000s

Instead of the following with tests.py:

Ran 14 tests in 1.875s

> Try with SVN r12254 because the test infrastructure was refactored in r12255
> and it still has some small rough edges.

I reverted back to r12254, same problem.

--
Olivier

Olivier Guilyardi

unread,
Jan 18, 2010, 5:04:06 PM1/18/10
to django...@googlegroups.com

Okay, I found the problem. In my tests I'm importing models, with something like:

from models import ...

That works when tests.py is located at the root of the app, that is: at the same
level as the models module.

But when splitting tests.py into individual files into tests/, models can't be
imported anymore. For it to work, I must use :

from my_app.models import ...

And now it works. The real problem here is that Django current behaviour is
silent failure, no error.

A little debugging led me in django.test.simple.get_tests() (at r12555 line 65):

try:
app_path = app_module.__name__.split('.')[:-1]
test_module = __import__('.'.join(app_path + [TEST_MODULE]), {}, {},
TEST_MODULE)
except ImportError, e:
# Couldn't import tests.py. Was it due to a missing file, or
# due to an import error in a tests.py that actually exists?
import os.path
from imp import find_module
try:
mod = find_module(TEST_MODULE, [os.path.dirname(app_module.__file__)])
except ImportError:
# 'tests' module doesn't exist. Move on.
test_module = None


Here the __import__ fails, not because the tests module is missing, but because
the tests module fails to import another module.

And then, the find_module() fallback fails too: my debugging shows that
os.path.dirname(app_module.__file__) points to /path/to/myapp/models and *not*
to /path/to/myapp as it should be.

This could be caused by the fact that my models are also split into models/,
thus dirname() returns a bogus value. But app_path above is correct, since it
doesn't rely on the fact the models module is a file...

Hum.. My head hurts ;)

--
Olivier

Matt Schinckel

unread,
Jan 20, 2010, 4:58:37 AM1/20/10
to Django users
On Jan 19, 7:21 am, Olivier Guilyardi <m...@xung.org> wrote:
> On 01/18/2010 10:04 PM, Ramiro Morales wrote:
>
>
>
>
>
> > On Mon, Jan 18, 2010 at 4:40 PM, Olivier Guilyardi <m...@xung.org> wrote:
> >> Hi,
>
> >> I'm trying to split tests.py into individual files into a tests/ subfolder, but
> >> that doesn't work.
>
> >> I correctly import everything from within tests/__init__.py, as previously said
> >> on this mailing list:
> >>http://groups.google.com/group/django-users/browse_frm/thread/10cfd65...

>
> >> But that doesn't work. I tested on Django 1.0.2 and SVN r12255, same thing.
>
> >> Any clue?
>
> > What do you mean with "doesn't work" ?. Do you get a traceback? No
> > error but your
> > tests aren't being run?, ...
>
> No error, the tests are not run. When splitting, it says:
>
> Ran 0 tests in 0.000s
>
> Instead of the following with tests.py:
>
> Ran 14 tests in 1.875s
>
> > Try with SVN r12254 because the test infrastructure was refactored in r12255
> > and it still has some small rough edges.
>
> I reverted back to r12254, same problem.

Drop into ./manage.py shell, and make sure you can import app.tests:
errors doing this will silently fail.

(May or may not solve your problem, but it has happened to me about a
dozen times today: I split my tests.py into:

tests/
__init__.py
integration/
__init__.py
*.py
unit/
__init__.py
*.py

and so on.)

Matt.

Olivier Guilyardi

unread,
Jan 20, 2010, 6:25:49 AM1/20/10
to django...@googlegroups.com
On 01/20/2010 10:58 AM, Matt Schinckel wrote:
> On Jan 19, 7:21 am, Olivier Guilyardi <m...@xung.org> wrote:
>> On 01/18/2010 10:04 PM, Ramiro Morales wrote:
>>
>>
>>> On Mon, Jan 18, 2010 at 4:40 PM, Olivier Guilyardi <m...@xung.org> wrote:
>>>> Hi,
>>>> I'm trying to split tests.py into individual files into a tests/ subfolder, but
>>>> that doesn't work.

[...]


>> No error, the tests are not run. When splitting, it says:
>>
>> Ran 0 tests in 0.000s
>>
>> Instead of the following with tests.py:
>>
>> Ran 14 tests in 1.875s
>>

[...]

> Drop into ./manage.py shell, and make sure you can import app.tests:
> errors doing this will silently fail.
>
> (May or may not solve your problem, but it has happened to me about a
> dozen times today: I split my tests.py into:
>
> tests/
> __init__.py
> integration/
> __init__.py
> *.py
> unit/
> __init__.py
> *.py
>
> and so on.)

Please see my last mail, this issue is resolved. Indeed it was import-related
and silently failing.

I believe this is a Django bug, although I got no comments to my last post.

--
Olivier

Matt Schinckel

unread,
Jan 20, 2010, 7:21:14 AM1/20/10
to Django users

Yes, it came through as I was replying :)

> I believe this is a Django bug, although I got no comments to my last post.

It is certainly annoying: tests just don't run, and the only way to
find out why is to drop into a shell and import the offending
module...

Might put some time into finding out why exceptions are not propagated
while importing tests in django.

Matt.

Olivier Guilyardi

unread,
Jan 20, 2010, 12:11:24 PM1/20/10
to django...@googlegroups.com
On 01/20/2010 01:21 PM, Matt Schinckel wrote:

> On Jan 20, 9:25 pm, Olivier Guilyardi <m...@xung.org> wrote:

[...]

>> Please see my last mail, this issue is resolved. Indeed it was import-related
>> and silently failing.
>
> Yes, it came through as I was replying :)

Then I think that it would be better to follow up on this other post of mine
(01/18/2010 11:04 PM).

>> I believe this is a Django bug, although I got no comments to my last post.
>
> It is certainly annoying: tests just don't run, and the only way to
> find out why is to drop into a shell and import the offending
> module...

Indeed, silent failures are truly frustrating. Noisy errors are a blessing in
comparison.

> Might put some time into finding out why exceptions are not propagated
> while importing tests in django.

I already spent some time debugging this, and explain what happens (and might
happen) in my previous post.

--
Olivier

Reply all
Reply to author
Forward
0 new messages