django アプリケーションテスト時のエラー


ImportError

ターミナル
$ python manage.py test

上記を実行時に
ImportError: 'tests' module incorrectly imported fromのエラーが発生。

原因

同じディレクトリ内に「tests」「tests.py」の両方が存在している事が原因。

diary
    ├── tests
    │   └── test_views.py
    └── tests.py

対処法

既存のtests.pyを削除することで解決。
「tests.py」についてはターミナルでstartupコマンドを実行した際に、自動的に作られてしまうので手動で削除する必要がある。

RuntimeError

上記エラー解消後に発生。
RuntimeError: Model class django.contrib.contenttypes.models.ContentType doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.

INSTALLED_APPS = [
    'django.contrib.contenttypes',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    'diary.apps.DiaryConfig',
    'accounts.apps.AccountsConfig',

    'allauth',
    'allauth.account',
]

settingsにはエラーに書かれているdjango.contrib.contenttypesは書かれているので別のものが原因と思われる。

原因&対処法

このページのコメント欄に書かれている同じディレクトリ内の__init__.pyを削除したところエラーが解消された。