Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3990500
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:30:06+00:00 2026-05-20T06:30:06+00:00

I m writing some code which retrieves info about installed apps, especially defined models,

  • 0

Im writing some code which retrieves info about installed apps, especially defined models, and then does stuff based on that information, but Im having some problems writing a clean, nice unittest. Is there a way to emulate or add an app in unittests without have to run manage.py startproject, manage.py startapp in my testsfolder to have a test app available for unittests?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T06:30:07+00:00Added an answer on May 20, 2026 at 6:30 am

    Sure, try this on for size:

    from django.conf import settings
    from django.core.management import call_command
    from django.test.testcases import TestCase
    from django.db.models import loading
    
    class AppTestCase(TestCase):
        '''
        Adds apps specified in `self.apps` to `INSTALLED_APPS` and
        performs a `syncdb` at runtime.
        '''
    
        apps = ()
        _source_installed_apps = ()
    
        def _pre_setup(self):
            super(AppTestCase, self)._pre_setup()
    
            if self.apps:
                self._source_installed_apps = settings.INSTALLED_APPS
                settings.INSTALLED_APPS = settings.INSTALLED_APPS + self.apps
                loading.cache.loaded = False
                call_command('syncdb', verbosity=0)
    
        def _post_teardown(self):
            super(AppTestCase, self)._post_teardown()
    
            if self._source_installed_apps:
                settings.INSTALLED_APPS = self._source_installed_apps
                self._source_installed_apps = ()
                loading.cache.loaded = False
    

    Your test case would look something like this:

    class SomeAppTestCase(AppTestCase):
    
        apps = ('someapp',)
    

    In case you were wondering why, I did an override of _pre_setup() and _post_teardown() so I don’t have to bother with calling super() in setUp() and tearDown() in my final test case. Otherwise, this is what I pulled out of Django’s test runner. I whipped it up and it worked, although I’m sure that, with closer inspection, you can further optimize it and even avoid calling syncdb every time if it won’t conflict with future tests.

    EDIT:

    So I seem to have gotten out of my way, thinking you need to dynamically add new models. If you’ve created an app for testing purposes only, here’s what you can do to have it discovered during your tests.

    In your project directory, create a test.py file that will contain your test settings. It should look something like this:

    from settings import *
    
    # registers test app for discovery
    INSTALLED_APPS += ('path.to.test.app',)
    

    You can now run your tests with python manage.py test --settings=myproject.test and your app will be in the installed apps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm thinking about writing some code in JavaScript which I can use in my
Since which version does the bytes() function exist in Python? I'm writing some code
I'm writing some code which could really do with some simple compile time metaprogramming.
I am writing some code in which i need to get a line from
I'm writing some code for a class constructor which loops through all the properties
I'm writing a JUnit test for some code that produces an Excel file (which
I'm writing code to download email from various servers, some of which are outside
I'm writing some code which is intended to: 1. Render into an OpenGL texture
Does anyone have some good hints for writing test code for database-backend development where
I am writing some code which maps LDAP property names to friendly names and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.