-1

I know this question was asked before, but I was wanting to see if there is a more updated solution. Would there be a way to load all my fixtures in setUp and and flush them when all tests are finished?

Right now, I'm loading in my fixtures like so...

from django.test import TestCase
from django.core.management import call_command

class GlobalSetup(TestCase):

    def setUp(self):
        # Load fixtures
        call_command('loaddata', 'test_cfst.json', verbosity=0)
        call_command('loaddata', 'test_lmt.json', verbosity=0)
        call_command('loaddata', 'test_qt.json', verbosity=0)

class BaseTest(GlobalSetup):
    fixtures = [
        'test_cfst.json',
        'test_lmt.json',
        'test_qt.json'
        ]

    def setUp(self):
        super(BaseTest, self).setUp()

    def test_base(self):
        # Some random tests

With the newer version of django is there a way, or better way, to do this?

DForsyth
  • 498
  • 8
  • 19
  • 1
    [This answer](https://stackoverflow.com/a/33400697/) to your linked question says that the fixtures are loaded once for each test class since Django 1.8. – Alasdair Jul 05 '18 at 15:15
  • Ah, I dug through the 1.8 updates, but I skimmed right over the [TestCase](https://docs.djangoproject.com/en/1.8/releases/1.8/#testcase-data-setup) part. My fault for not triple checking. Thanks for pointing it out. – DForsyth Jul 05 '18 at 15:23

1 Answers1

2

I am not sure that you are aware or not but you just load fixtures as below:

from django.test import TestCase
from myapp.models import Animal

class AnimalTestCase(TestCase):
    fixtures = ['mammals.json', 'birds']

    def setUp(self):
        # Test definitions as before.
        call_setup_methods()

    def test_fluffy_animals(self):
        # A test that uses the fixtures.
        call_some_test_code()

example from the docs

So you don't need to write GlobalSetup with call_command as you did in your current example which is leading to load the fixtures twice. because the method is already called in setUpClass(refer this link)

Gahan
  • 4,075
  • 4
  • 24
  • 44
  • I was not and this does help! I've just been trying to find tutorials on testing to better learn it, and was following [this one](http://blog.namis.me/2012/02/13/loading-fixtures-in-django-tests/) but it's a bit out of date. I appreciate the answer! – DForsyth Jul 05 '18 at 15:42
  • 1
    almost half decade older it is though.. that's why for starters you should follow documentation. and I am not aware of any such enrich documentation maintained as maintained by django. At some point it will also point you out about future deprecation too. and for upgrading django you should hence upgrade existing your project django version one by one and resolve the compatibility – Gahan Jul 05 '18 at 15:46
  • Will do! I was looking at several of the docs, but always like to see if I can find some used examples to reference from. Thanks for all the advice! – DForsyth Jul 05 '18 at 17:34