151π
β
I believe the reason is that your setUpClass(cls)
class method is not calling super. Because of that, django.tests.TestCase.setUpClass
is not called and
cls.cls_atomics = cls._enter_atomics()
is not called, naturally causing cls_atomics
to be undefined.
You should add super(ATestTests, cls).setUpClass()
to your setUpClass
.
π€Jorge Leitao
45π
For Django 1.8+, you should use TestCase.setUpTestData
instead of TestCase.setUpClass
.
class MyTests(TestCase):
@classmethod
def setUpTestData(cls):
# Set up data for the whole TestCase
cls.foo = Foo.objects.create(bar="Test")
def test1(self):
self.assertEqual(self.foo.bar, 'Test')
The documentation is here.
π€seddonym
- [Django]-When to use Serializer's create() and ModelViewset's perform_create()
- [Django]-Django.contrib.auth.logout in Django
- [Django]-Django-Forms with json fields
7π
I had a similar problem where a TestCase
used setUpClass
but did not have a tearDownClass
method. My tests pass when I add an empty one:
@classmethod
def tearDownClass(cls):
pass
I also do not call django.setup
.
π€Matt
- [Django]-How to override css in Django Admin?
- [Django]-How to migrate back from initial migration in Django 1.7?
- [Django]-How do I do an OR filter in a Django query?
-1π
Here is the complete code with the call to the base class (as suggested by @J. C. LeitΓ£o):
import django
import unittest
from django.test import TestCase
import logging
import sys
from builtins import classmethod
class ATestTests(TestCase):
@classmethod
def setUpClass(cls):
super(ATestTests, cls).setUpClass()
django.setup()
logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
def setUp(self):
self._app = Application(name="a")
def testtest(self):
self.assertIsNotNone(self._app)
π€habakuk
- [Django]-Python (and Django) best import practices
- [Django]-In Django β Model Inheritance β Does it allow you to override a parent model's attribute?
- [Django]-What is a NoReverseMatch error, and how do I fix it?
Source:stackexchange.com