Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fixtures not loaded during testing

I wrote a unit test checking whether initial data is loaded correctly. However the Node.objects.all().count() always returns 0, thus it seems as the fixtures are not loaded at all. There is no output/error msg in the command line that fixtures are not loaded.

from core.models import Node

class NodeTableTestCase(unittest.TestCase):
    fixtures = ['core/core_fixture.json']
    def setUp(self):
        print "nothing to prepare..."

    def testFixture(self):
        """Check if initial data can be loaded correctly"""
        self.assertEqual(Node.objects.all().count(), 14) 

the fixture core_fixture.json contains 14 nodes and I'm using this fixture as a initial data load into the db using the following command:

python manage.py loaddata core/core_fixture.json

They are located in the folder I provided in the settings.py setting FIXTURE_DIRS.

like image 308
Thomas Kremmel Avatar asked Jul 22 '12 21:07

Thomas Kremmel


People also ask

What are fixtures in test cases?

A test fixture is an environment used to consistently test some item, device, or piece of software. Test fixtures can be found when testing electronics, software and physical devices.

How do you use test fixtures?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.

What are fixtures in typescript?

Fixtures are a thin abstraction layer over sample data in your application which allows for better organizing, often complex, data structures representing different entities.


2 Answers

Found the solution in another thread, answer from John Mee

# Import the TestCase from django.test:

# Bad:  import unittest
# Bad:  import django.utils.unittest
# Good: import django.test

from django.test import TestCase

class test_something(TestCase):
    fixtures = ['one.json', 'two.json']
    ...

Doing this I got a proper error message, saying that foreign key is violated and I had to also include the fixtures for the app "auth". I exported the needed data with this command:

manage.py dumpdata auth.User auth.Group > usersandgroups.json

Using Unittest I got only the message that loading of fixture data failed, which was not very helpful.

Finally my working test looks like this:

from django.test import TestCase

class NodeTableTestCase2(TestCase):
    fixtures = ['auth/auth_usersandgroups_fixture.json','core/core_fixture.json']

    def setUp(self):
        # Test definitions as before.
        print "welcome in setup: while..nothing to setup.."

    def testFixture2(self):
        """Check if initial data can be loaded correctly"""
        self.assertEqual(Node.objects.all().count(), 11)  
like image 152
Thomas Kremmel Avatar answered Oct 22 '22 12:10

Thomas Kremmel


When loading fixtures in test cases, I don't think Django allows you to include the directory name. Try changing your fixtures setting to:

fixtures = ['core_fixture.json',]

You might have to change your FIXTURE_DIRS setting as well, to include the core directory.

If you run your tests in verbose mode, you will see the fixture files that Django attempts to load. This should help you debug your configuration.

python manage.py test -v 2
like image 1
Alasdair Avatar answered Oct 22 '22 12:10

Alasdair