I has a script that will generate a JSON file (let me call it data.json
) that for my django application, usually I can test it by running command
python manage.py testserver data.json
However, I would like to run this thing in unit tests rather than run this through shell (because it would start a server and never return back to the shell). I don't need to run any tests that depends on this fixture. I only want to make sure that the fixture generated can be loaded.
Django's own TestCase
supports automatically setting up and tearing down fixtures via the class level fixtures
attribute. e.g.
from django.test import TestCase
class MyTest(TestCase):
# Must live in <your_app>/fixtures/data.json
fixtures = ['data.json']
def test_something(self):
# When this runs, data.json will already have been loaded
...
However, since you just want to check that the fixture can be loaded rather than using it as part of the test, then you could just invoke the loaddata
command somewhere in your test code.
e.g.
from django.core.management import call_command
call_command('loaddata', '/path/to/data.json')