Update to Django 1.8 - AttributeError: django.test.TestCase has no attribute 'cls_atomics'

*爱你&永不变心* 提交于 2019-12-04 22:11:36

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.

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.

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.

habakuk

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)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!