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.