How to write unit tests for the new 3rd party Django library

I am making a library as a Django app that is designed to provide common leaderboards for other app models in a game app I'm working on. Clients of my library need to extend the abstract base class that I create and override methods to provide code for actually aggregating objects.

I want to keep this diagramming app standalone and loosely coupled so that I can easily unwind it and open it open source and so I am trying to write unit tests that do not depend on any models outside of the apps, even though the whole purpose of the app is to essentially combine data from models in third-party applications. I could just create a dummy model in my own application models.py

, but then that would create an unused table for every project that uses the library, which seems imperfect. Is there a perfect way to do this?

+3


source to share


1 answer


So far, I see two options.

Option 1: Create unmanaged model classes and manually create and destroy database tables in your tests.



Option 2 (which I chose): declare test models in my module tests

. Follow the guidelines here and call syncdb

in a method setUp

to create tables. Adapted for testing, this is what it looks like:

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

class TestModel(models.Model):
    data = models.FloatField()

    class Meta:
        app_label = 'myapp'

class LibraryTests(TestCase):
    def setUp(self):
        super(LibraryTests, self).setUp()
        models.register_models('myapp', TestModel)
        call_command('syncdb')

      

+3


source







All Articles