4

I've been really enjoying the convenience of setUpTestData and --keepdb in Django 1.8!

However, I am running into an issue keeping data consistent across multiple test files. I'd like to have my setUpTestData classmethod in one location, and have each of my test files reference it, so that I don't have to copy/paste changes into each individual test file.

I'm a little confused about how to go about this, specifically with regards to the classmethod which seems to prevent me from importing my setUpTestData function from another file. Can someone help me? Thanks ahead!

Current test file

from django.test import TestCase
from models import SpecificModel 

class TestData(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.test_item = SpecificModel.objects.create(data="some data")

SetupData file

???
Adam Starrh
  • 6,428
  • 8
  • 50
  • 89
  • This questions is a bit confusing, which I think is why it hasn't been answered. Can you just subclass TestData in your other files to get 'setUpTestData'? – Mark Galloway Jul 19 '15 at 22:25
  • That's what I need help with. I was fiddling with it for about 45 minutes, but I don't know how where to place the @classmethod to get it to work properly. – Adam Starrh Jul 19 '15 at 22:29
  • Well I fiddled around with it a little more, and got it working this time. I think my problem was that I was trying to pass `self` as an argument and not `cls`. Thanks for pointing me in the right direction! – Adam Starrh Jul 19 '15 at 22:39

2 Answers2

6

Can you just inherit the TestData class which declares the method?

base_tests.py

from django.test import TestCase
from models import SpecificModel 

class TestData(TestCase):

    @classmethod
    def setUpTestData(cls):
        cls.test_item = SpecificModel.objects.create(data="some data")

specific_tests.py

from .base_tests import TestData

class SubclassOfTestData(TestData):
    # Inherits `setUpTestData`
    pass
Mark Galloway
  • 4,050
  • 19
  • 26
  • This is a lot easier than what I was doing.. Thanks for taking the time to help! – Adam Starrh Jul 19 '15 at 22:47
  • 2
    This is a nice solution and it works when you only have one sub class of TestData. If you have two different classes that inherits from TestData then the database does not seem to be destroyed between tests in the two different classes. This results in setUpTestData to be called twice in the DB which is not what one (at least me) would expect. – KlausCPH Jan 28 '17 at 15:13
  • @KlausCPH did you find a way for inhering from multiple classes and executing setuptestdata only once – Aseem Jun 07 '20 at 01:31
  • @aseem, no I did not. – KlausCPH Jun 18 '20 at 15:58
1

I think the below works well and overcomes the issues @KlausCPH and @Aseem were having. I'd rather comment on Mark's answer but do not have enough reputation.

base_tests.py

from django.test import TestCase
from models import SpecificModel 

class TestData(TestCase):

    def setUp(self):
        self.test_item = SpecificModel.objects.create(data="some data")

specific_tests.py

from .base_tests import TestData

class SubclassOfTestData(TestData):

    def setUp(self):
        super(SubclassOfTestData, self).setUp()
        # any other setup specific for this class
        # eg: self.foo = 'bar'

    def test_etc(self):
        pass