Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No fixtures found when testing an app on Django

I have finally decided to create unit test for all my code. That is I'm trying to create fixtures based on my models to test my function against them.

I have create a json fixture using dumpdata command and placed it under fixtures directory on my app. The following is the code on my test:

import unittest
from mysite.myapp.models import Post

class RatingTestCase(unittest.TestCase):
  fixtures = [
      'posts.json',
  ]

def test_me(self):
  p = Post.objects.all()
  self.assertTrue(p)

I run my test using the following command on my Arch Linux machine:

python2.7 manage.py test myapp

It creates a sqlite database and installs all the tables and indeces, however at the end it says no fixtures found and it says my test failed since it didn't find any data.

I'm running the latest revision of development version of Django and noticed that based on the documentation I am supposed to import unittest using:

from django.utils import unittest

However, when I do this, it complains that unittest cannot be imported. That's why I import unittest directly from my python path which worked.

I've tried to mock django model objects before, but I think it's better to test Django apps using fixtures than using mock libraries. Any idea how to load the fixtures?

Thanks in advance.

EDIT: If I change my fixture name to initial_data.json, it will load it every time I run my test. However, I still need to have multiple fixture names for running different tests.

EDIT: I got it working by importing TestCase from the following:

from django.test import TestCase
like image 953
mohi666 Avatar asked Oct 12 '22 01:10

mohi666


1 Answers

There are a couple things that could help:

  1. You can run the test with --verbosity=2 to see which directories django looks in for fixtures.
  2. django TestCase is what django recommends to use, opposed to unittest.
  3. Where is your fixture directory/file located? If it's not in your app you have to specify a fixture directory in your settings file.
like image 60
dm03514 Avatar answered Oct 14 '22 04:10

dm03514