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
http://ericholscher.com/blog/2008/nov/4/introduction-pythondjango-testing-basic-unit-tests/
Shawn
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"
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
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
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
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
$ 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
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
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
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.
[...]
>> 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
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.
> 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