Running Django tests in PyCharm for standalone apps

Over the last couple of years I have created several Django projects designed to be standalone apps. While it’s not to hard to create a custom test running to handle unit tests using:

django.core.management.call_command('test', APP_TO_TEST)

This technique unfortunately does not integrate very well into PyCharm’s built in testing tools (you also cannot debug).

You can however trick PyCharm into thinking it is running a Django project and everything works!

Create a manage.py file in the root of your project inside add the following:

# coding=utf-8
# Helper test runner for running tests for django standalone apps
import os
from optparse import OptionParser
from django.conf import settings

APP_TO_TEST = 'warthog'

parser = OptionParser()
parser.add_option('--DATABASE_ENGINE', dest='DATABASE_ENGINE', default='django.db.backends.sqlite3')
parser.add_option('--DATABASE_NAME', dest='DATABASE_NAME', default='test.db')parser.add_option('--DATABASE_USER', dest='DATABASE_USER', default='')parser.add_option('--DATABASE_PASSWORD', dest='DATABASE_PASSWORD', default='')
parser.add_option('--DATABASE_HOST', dest='DATABASE_HOST', default='')
parser.add_option('--DATABASE_PORT', dest='DATABASE_PORT', default='')
parser.add_option('--SITE_ID', dest='SITE_ID', type='int', default=1)
options, args = parser.parse_args()
settings.configure(**{
 'DATABASES': {
 'default': {
 'ENGINE': options.DATABASE_ENGINE,
 'NAME': options.DATABASE_NAME,
 'USER': options.DATABASE_USER,
 'PASSWORD': options.DATABASE_PASSWORD,
 'HOST': options.DATABASE_HOST,
 'PORT': options.DATABASE_PORT,
 },
 },
 'SITE_ID': options.SITE_ID,
 'ROOT_URLCONF': '',
 'TEMPLATE_LOADERS': (
 'django.template.loaders.filesystem.load_template_source',
 'django.template.loaders.app_directories.load_template_source',
 ),
 'TEMPLATE_DIRS': (
 os.path.join(os.path.dirname(__file__), 'templates'),
 ),
 'INSTALLED_APPS': (
 "django.contrib.auth",
 "django.contrib.contenttypes",
 "django.contrib.sessions",
 "django.contrib.sites",
 APP_TO_TEST,
 ),
})

Now enable Django support pointing at manage.py, create a Django Unit test and your good to go.

Advertisements

5 Comments

  1. knoppo

    The pycharm django tests are executing every subclass of “unittest.TestCase“ just like `./manage.py test`.
    pycharm lets you set an alternative settings file for django tests.
    Put one in /tests folder.
    In the django test run configuration put as target and select your test settings file. This way you get the very exciting testing features of pycharm 😉

    • Seems things have changed a little since I last tried this. PyCharm does now execute the Django test runner using:
      django.core.management.call_command(‘test’, TARGET)

      One thing though, don’t put your settings file in your test folder or you might get hit with circular dependencies. Safer to just use a test_settings.py file in your project root. The other bonus is that this should be easier to integrate into CI for automating test cases.

      • yea you’re right, I just played a bit more with it, beacause I wanted to test my reusable apps completely standalone and I have to correct me: as manage.py I select my test_settings.py file and mark the project root as source root. In the test configuration define a environment var DJANGO_SETTINGS_MODULE = ‘myapp.test_settings’, set the working directory to the project root and it should work

        before I had to mark my test dir as source root to make it working I’m still a bit confused whats actually going on behind the scenes 😉

      • That sounds about right, when you mark a folder as a source root the folder is added to the python path by PyCharm when a unittest or application is run.

  2. knoppo

    oh no it stripped it… put your app_name as target

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: