1

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.

Hsiao Yi
  • 117
  • 2
  • 13

2 Answers2

4

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')
Will Keeling
  • 22,055
  • 4
  • 51
  • 61
  • Well, that means the fixture will be actually loaded into real database, which I would like to avoid. – Hsiao Yi Dec 05 '18 at 11:08
  • @HsiaoYi so you wish to load it into a different database? You have a specific database configured for that? – Will Keeling Dec 05 '18 at 11:12
  • I would like to have that be loaded into a test database, but would not start a test server. One thing I am thinking right now is to use the fundamental mechanism without running the server (https://github.com/django/django/blob/fbc7e4138921d0fbee823ce6ea9774de66d27e47/django/core/management/commands/testserver.py#L30-L37), but I am afraid I miss something. – Hsiao Yi Dec 05 '18 at 11:15
  • If your unit test extends Django's `TestCase`, then this would all happen for you when you run your test. Django would insert the fixture data into the test database before the test starts. – Will Keeling Dec 05 '18 at 11:22
  • Do you mean I should `call_command('loaddata', ...)` inside a unit test that extends `TransactionTestCase` ? – Hsiao Yi Dec 05 '18 at 11:26
  • Well you wouldn't have to call `loaddata` at all. You could set the `fixtures` attribute to `['data.json']` and then let Django automatically load the data into the test database before the test runs. The `data.json` file would need to be placed in a folder called `fixtures` inside your app. I've added an example to the answer. – Will Keeling Dec 05 '18 at 11:36
  • You should probably also use `django.test.TestCase` which inherits from `TransactionTestCase` (as shown in the example) – Will Keeling Dec 05 '18 at 12:11
0

Django management commands can be run in your code using the call_commands.

from django.core.management import call_command
from django.core.management.commands import testserver

call_command('testserver', 'data.json')
Arunkumar
  • 81
  • 4