import sys

try:
    from django.conf import settings
    from django.test.utils import get_runner

    settings.configure(
        DEBUG=True,
        USE_TZ=True,
        DATABASES={
            "default": {
                "ENGINE": "django.db.backends.sqlite3",
                "NAME": ":memory:",
            }
        },
        INSTALLED_APPS=[
            "django.contrib.auth",
            "django.contrib.contenttypes",
            "django.contrib.staticfiles",
            "django.contrib.sites",
            "chartit",
            "chartit_tests",
        ],
        SITE_ID=1,
        MIDDLEWARE_CLASSES=(),
        STATIC_URL='/static/'
    )

    try:
        import django
        setup = django.setup
    except AttributeError:
        pass
    else:
        setup()

except ImportError:
    import traceback
    traceback.print_exc()
    raise ImportError("To fix this error, run: pip install -r requirements.txt")


def run_tests(*test_args):
    if not test_args:
        test_args = ["chartit_tests"]

    # Run tests
    TestRunner = get_runner(settings)
    test_runner = TestRunner()

    failures = test_runner.run_tests(test_args)

    if failures:
        sys.exit(bool(failures))


if __name__ == "__main__":
    run_tests(*sys.argv[1:])

import os
import sys

try:
    sys.path.append('demoproject')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demoproject.settings")

    from django.conf import settings
    from django.core.management import call_command

    settings.DATABASES['default']['NAME'] = ':memory:'
    settings.INSTALLED_APPS.append('chartit_tests')

    try:
        import django
        setup = django.setup
    except AttributeError:
        pass
    else:
        setup()

except ImportError:
    import traceback
    traceback.print_exc()
    raise ImportError("To fix this error, run: pip install -r requirements.txt")


def run_tests(*test_args):
    if not test_args:
        test_args = ["chartit_tests"]

    # ./manage.py test takes care of database creation and
    # application of migrations if any
    result = call_command('test', *test_args, verbosity=2, failfast=True)
    sys.exit(result)


if __name__ == "__main__":
    run_tests(*sys.argv[1:])

Load DB migrations before testing and use verbose=2 and failfast
Load DB migrations before testing and use verbose=2 and failfast

Note that we use `manage.py test` instead of
`manage.py migrate` and manually running the tests. This
lets Django take care of applying migrations before running tests.
This works around https://code.djangoproject.com/ticket/22487
which causes a test failure on Django 1.8.14.

In 1.8.14 somehow we end up without any actual data in the test DB
and one of the tests fails if we use `manage.py migrate` and run the
tests manually via TestRunner.

+ import os
  import sys
  
  try:
+     sys.path.append('demoproject')
+     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demoproject.settings")
+ 
      from django.conf import settings
-     from django.test.utils import get_runner
+     from django.core.management import call_command
  
+     settings.DATABASES['default']['NAME'] = ':memory:'
+     settings.INSTALLED_APPS.append('chartit_tests')
-     settings.configure(
-         DEBUG=True,
-         USE_TZ=True,
-         DATABASES={
-             "default": {
-                 "ENGINE": "django.db.backends.sqlite3",
-                 "NAME": ":memory:",
-             }
-         },
-         INSTALLED_APPS=[
-             "django.contrib.auth",
-             "django.contrib.contenttypes",
-             "django.contrib.staticfiles",
-             "django.contrib.sites",
-             "chartit",
-             "chartit_tests",
-         ],
-         SITE_ID=1,
-         MIDDLEWARE_CLASSES=(),
-         STATIC_URL='/static/'
-     )
  
      try:
          import django
          setup = django.setup
      except AttributeError:
          pass
      else:
          setup()
  
  except ImportError:
      import traceback
      traceback.print_exc()
      raise ImportError("To fix this error, run: pip install -r requirements.txt")
  
  
  def run_tests(*test_args):
      if not test_args:
          test_args = ["chartit_tests"]
  
+     # ./manage.py test takes care of database creation and
+     # application of migrations if any
+     result = call_command('test', *test_args, verbosity=2, failfast=True)
+     sys.exit(result)
-     # Run tests
-     TestRunner = get_runner(settings)
-     test_runner = TestRunner()
- 
-     failures = test_runner.run_tests(test_args)
- 
-     if failures:
-         sys.exit(bool(failures))
  
  
  if __name__ == "__main__":
      run_tests(*sys.argv[1:])
  
Load DB migrations before testing and use verbose=2 and failfast
## Code Before:
import sys

try:
    from django.conf import settings
    from django.test.utils import get_runner

    settings.configure(
        DEBUG=True,
        USE_TZ=True,
        DATABASES={
            "default": {
                "ENGINE": "django.db.backends.sqlite3",
                "NAME": ":memory:",
            }
        },
        INSTALLED_APPS=[
            "django.contrib.auth",
            "django.contrib.contenttypes",
            "django.contrib.staticfiles",
            "django.contrib.sites",
            "chartit",
            "chartit_tests",
        ],
        SITE_ID=1,
        MIDDLEWARE_CLASSES=(),
        STATIC_URL='/static/'
    )

    try:
        import django
        setup = django.setup
    except AttributeError:
        pass
    else:
        setup()

except ImportError:
    import traceback
    traceback.print_exc()
    raise ImportError("To fix this error, run: pip install -r requirements.txt")


def run_tests(*test_args):
    if not test_args:
        test_args = ["chartit_tests"]

    # Run tests
    TestRunner = get_runner(settings)
    test_runner = TestRunner()

    failures = test_runner.run_tests(test_args)

    if failures:
        sys.exit(bool(failures))


if __name__ == "__main__":
    run_tests(*sys.argv[1:])

## Instruction:
Load DB migrations before testing and use verbose=2 and failfast
## Code After:
import os
import sys

try:
    sys.path.append('demoproject')
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "demoproject.settings")

    from django.conf import settings
    from django.core.management import call_command

    settings.DATABASES['default']['NAME'] = ':memory:'
    settings.INSTALLED_APPS.append('chartit_tests')

    try:
        import django
        setup = django.setup
    except AttributeError:
        pass
    else:
        setup()

except ImportError:
    import traceback
    traceback.print_exc()
    raise ImportError("To fix this error, run: pip install -r requirements.txt")


def run_tests(*test_args):
    if not test_args:
        test_args = ["chartit_tests"]

    # ./manage.py test takes care of database creation and
    # application of migrations if any
    result = call_command('test', *test_args, verbosity=2, failfast=True)
    sys.exit(result)


if __name__ == "__main__":
    run_tests(*sys.argv[1:])
