Skip to content

Commit

Permalink
Fixed #19941 -- Modified runtests.py to make running the tests easier.
Browse files Browse the repository at this point in the history
1. Automatically use tests/../django as the Django version.
2. If settings aren't provided through --settings or DJANGO_SETTINGS_MODULE)
   then use test_sqlite.
  • Loading branch information
akaariai authored and timgraham committed Jul 24, 2013
1 parent 372158a commit b2314d9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
16 changes: 13 additions & 3 deletions docs/internals/contributing/writing-code/unit-tests.txt
Expand Up @@ -25,16 +25,26 @@ Quickstart
~~~~~~~~~~

Running the tests requires a Django settings module that defines the
databases to use. To make it easy to get started, Django provides a
sample settings module that uses the SQLite database. To run the tests
with this sample ``settings`` module:
databases to use. To make it easy to get started, Django provides and uses a
sample settings module that uses the SQLite database. To run the tests:

.. code-block:: bash

git clone git@github.com:django/django.git django-repo
cd django-repo/tests
./runtests.py

.. versionchanged:: 1.7

Older versions of Django required running the tests like this::

PYTHONPATH=..:$PYTHONPATH python ./runtests.py --settings=test_sqlite

``runtests.py`` now uses the Django package found at ``tests/../django`` (there
isn't a need to add this on your ``PYTHONPATH``) and ``test_sqlite`` for the
settings if settings aren't provided through either ``--settings`` or
:envvar:`DJANGO_SETTINGS_MODULE`.

.. _running-unit-tests-settings:

Using another ``settings`` module
Expand Down
20 changes: 16 additions & 4 deletions tests/runtests.py
Expand Up @@ -7,6 +7,20 @@
import tempfile
import warnings

def upath(path):
"""
Separate version of django.utils._os.upath. The django.utils version isn't
usable here, as upath is needed for RUNTESTS_DIR which is needed before
django can be imported.
"""
if sys.version_info[0] != 3 and not isinstance(path, bytes):
fs_encoding = sys.getfilesystemencoding() or sys.getdefaultencoding()
return path.decode(fs_encoding)
return path

RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__)))
sys.path.insert(0, os.path.dirname(RUNTESTS_DIR)) # 'tests/../'

from django import contrib
from django.utils._os import upath
from django.utils import six
Expand All @@ -15,7 +29,6 @@

TEST_TEMPLATE_DIR = 'templates'

RUNTESTS_DIR = os.path.abspath(os.path.dirname(upath(__file__)))
CONTRIB_DIR = os.path.dirname(upath(contrib.__file__))

TEMP_DIR = tempfile.mkdtemp(prefix='django_')
Expand Down Expand Up @@ -331,10 +344,9 @@ def paired_tests(paired_test, options, test_labels):
options, args = parser.parse_args()
if options.settings:
os.environ['DJANGO_SETTINGS_MODULE'] = options.settings
elif "DJANGO_SETTINGS_MODULE" not in os.environ:
parser.error("DJANGO_SETTINGS_MODULE is not set in the environment. "
"Set it or use --settings.")
else:
if "DJANGO_SETTINGS_MODULE" not in os.environ:
os.environ['DJANGO_SETTINGS_MODULE'] = 'test_sqlite'
options.settings = os.environ['DJANGO_SETTINGS_MODULE']

if options.liveserver is not None:
Expand Down

0 comments on commit b2314d9

Please sign in to comment.