Test fixtures

80 views
Skip to first unread message

Dan

unread,
Apr 15, 2010, 3:32:16 PM4/15/10
to Django users
Hi,
I'm pretty new to Django, and I'm trying to create a few simple tests
for my project. I've created a very basic fixture and I'm trying to
just load it and see that the data is there. Nothing big. I've read
the documentation here:
http://docs.djangoproject.com/en/dev/topics/testing/
but I must be missing something because I can't get it to work. This
is how I have my app set up:
articles/
models.py
tests.py
fixtures/
articles-test.json

tests.py looks like this:
...
class ArticleTests(unittest.TestCase):
fixtures = ['articles/fixtures/articles-test.json']

def test_article_exists(self):
a = Article.objects.get(pk=1)
self.assertEquals(headline='foo')

When I run manage.py test articles, it's finding my test and running,
but it fails with
...
DoesNotExist: Article matching query does not exist.

One thing that is confusing me is that although the debug messages say
the test db tables are being created, etc, it says nothing about
whether it's loading my fixture, or whether it's trying and failing.
The documentation is unclear about exactly where the fixtures need to
be located, so I've tried putting them in a lot different places, with
no luck. I tried mangling the json file, so if it found it it should
throw a parse error, and still there was no difference, so I think the
problem must be that it's not loading the fixture, but I just can't
figure out how to point it in the right directions.

Any ideas? Thanks.

Karen Tracey

unread,
Apr 15, 2010, 4:07:53 PM4/15/10
to django...@googlegroups.com
On Thu, Apr 15, 2010 at 3:32 PM, Dan <mrda...@gmail.com> wrote:
Hi,
I'm pretty new to Django, and I'm trying to create a few simple tests
for my project. I've created a very basic fixture and I'm trying to
just load it and see that the data is there. Nothing big. I've read
the documentation here:
http://docs.djangoproject.com/en/dev/topics/testing/
but I must be missing something because I can't get it to work. This
is how I have my app set up:
articles/
 models.py
 tests.py
 fixtures/
   articles-test.json

tests.py looks like this:
...
class ArticleTests(unittest.TestCase):
   fixtures = ['articles/fixtures/articles-test.json']

Remove the 'articles/fixtures/' part. Notice there are no paths in the the example doc -- fixtures directories under each app listed in INSTALLED_APPS are automatically searched for fixtures.
 
   def test_article_exists(self):
       a = Article.objects.get(pk=1)
       self.assertEquals(headline='foo')

When I run manage.py test articles, it's finding my test and running,
but it fails with
...
DoesNotExist: Article matching query does not exist.

One thing that is confusing me is that although the debug messages say
the test db tables are being created, etc, it says nothing about
whether it's loading my fixture, or whether it's trying and failing.
The documentation is unclear about exactly where the fixtures need to
be located, so I've tried putting them in a lot different places, with
no luck. I tried mangling the json file, so if it found it it should
throw a parse error, and still there was no difference, so I think the
problem must be that it's not loading the fixture, but I just can't
figure out how to point it in the right directions.

You are right, there is no diagnostic message issued when a fixture named in fixtures isn't found. There may be a ticket open on that. As for where the file needs to be...the test doc you point to does say "Once you've created a fixture and placed it in a fixtures directory in one of your INSTALLED_APPS, you can use it in your unit tests..." -- does that not make it clear where the file needs to go?

Karen

Dan

unread,
Apr 15, 2010, 5:11:01 PM4/15/10
to Django users
Thanks. Actually, that was the first thing I tried. I think I saw some
other example online where someone had the path to the fixture in the
code, so I just started trying anything. You're right about the
documentation, though -- it is pretty clear about the location of the
file. That just didn't work.

I did find this ticket:
http://code.djangoproject.com/ticket/12682

So I added a --verbosity=2 switch, and saw it was trying to load
initial_data.json. I changed my filename to that, and that works. I
still don't know why it ignores articles-test.json, but at least I've
got something working now.

Thanks for your help.

On Apr 15, 4:07 pm, Karen Tracey <kmtra...@gmail.com> wrote:

Dan

unread,
Apr 15, 2010, 5:19:43 PM4/15/10
to Django users
OK, just as I suspected, I was doing something silly. I was
subclassing TestCase from unittest instead of django.test. I fixed
that, and all is well now. Thanks again for the help.
Reply all
Reply to author
Forward
0 new messages