Lamps not loaded during testing

I wrote a unit test to verify that the source data was loading correctly. However, it Node.objects.all().count()always returns 0, so it seems that the lights do not load at all. There is no exit / error message on the command line that does not load.

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 device core_fixture.jsoncontains 14 nodes, and I use this device as the initial data load in db using the following command:

python manage.py loaddata core/core_fixture.json

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

+5
source share
3 answers

Found a solution in a different 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']
    ...

, , , , "auth". :

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

Unittest , , .

, :

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)  
+5

, Django . fixtures :

fixtures = ['core_fixture.json',]

, FIXTURE_DIRS, core.

, , Django . .

python manage.py test -v 2
+1

Make sure your application is listed in INSTALLED_APPSand that your application contains a models.pyfile.

+1
source

All Articles