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
There are a couple things that could help:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With