Django SOLUTION: TEST_DATABASE_PREFIX TypeError: can only concatenate str (not "NoneType") to str

Created:

The error

When you execute the django unit tests you get the error:

 Traceback (most recent call last):
  File "/opt/.pycharm_helpers/pycharm/django_test_manage.py", line 168, in <module>
    utility.execute()
  File "/opt/.pycharm_helpers/pycharm/django_test_manage.py", line 142, in execute
    _create_command().run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/commands/test.py", line 23, in run_from_argv
    super().run_from_argv(argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
    output = self.handle(*args, **options)
  File "/opt/.pycharm_helpers/pycharm/django_test_manage.py", line 104, in handle
    failures = TestRunner(test_labels, **options)
  File "/opt/.pycharm_helpers/pycharm/django_test_runner.py", line 255, in run_tests
    extra_tests=extra_tests, **options)
  File "/opt/.pycharm_helpers/pycharm/django_test_runner.py", line 156, in run_tests
    return super(DjangoTeamcityTestRunner, self).run_tests(test_labels, extra_tests, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/django/test/runner.py", line 684, in run_tests
    old_config = self.setup_databases(aliases=databases)
  File "/usr/local/lib/python3.7/site-packages/django/test/runner.py", line 606, in setup_databases
    self.parallel, **kwargs
  File "/usr/local/lib/python3.7/site-packages/django/test/utils.py", line 156, in setup_databases
    test_databases, mirrored_aliases = get_unique_databases_and_mirrors(aliases)
  File "/usr/local/lib/python3.7/site-packages/django/test/utils.py", line 257, in get_unique_databases_and_mirrors
    default_sig = connections[DEFAULT_DB_ALIAS].creation.test_db_signature()
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 295, in test_db_signature
    self._get_test_db_name(),
  File "/usr/local/lib/python3.7/site-packages/django/db/backends/base/creation.py", line 153, in _get_test_db_name
    return TEST_DATABASE_PREFIX + self.connection.settings_dict['NAME']
TypeError: can only concatenate str (not "NoneType") to str

Process finished with exit code 1

Solution

You need to specify a sqlite ENGINE when using unit tests. Open the settings.py and add the just after DATABASES section:

import sys
if 'test' in sys.argv or 'test_coverage' in sys.argv: #Covers regular testing and django-coverage
    DATABASES['default']['ENGINE'] = 'django.db.backends.sqlite3'
    DATABASES['default']['NAME'] = ':memory:'