Django: Keeping setUpTestData DRY
Asked Answered
L

2

5

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

???
Lithology answered 9/7, 2015 at 18:41 Comment(3)
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'?Manvel
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.Lithology
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!Lithology
M
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
Manvel answered 19/7, 2015 at 22:38 Comment(4)
This is a lot easier than what I was doing.. Thanks for taking the time to help!Lithology
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.Humphries
@Humphries did you find a way for inhering from multiple classes and executing setuptestdata only onceSwagman
@aseem, no I did not.Humphries
R
2

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
Romanticize answered 26/4, 2022 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.