Django v2.2
Я пишу свой контрольный пример вот уже 2 года, вот так:
from django.test import override_settings
from rest_framework.test import APIClient, APITestCase
from someapp.tests.factories import (
OrganizationFactory,
)
class SomeTestCase(APITestCase):
def setUp(self):
self.maxDiff = None
self.organization = OrganizationFactory(name="ENP", domain="localhost")
@override_settings(ALLOWED_HOSTS=["*"])
def test_something(self):
self.assertTrue(True)
затем запускаю
python manage.py test logical.path.to.SomeTestCase.test_something --keepdb
--keepdb
состоит в том, чтобы избежать перебора тестовой базы данных и восстановления с нуля
My config.settings.test.py
is
"""
With these settings, tests run faster.
"""
from .base import * # noqa
from .base import env
# GENERAL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#debug
DEBUG = False
# https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
SECRET_KEY = env("DJANGO_SECRET_KEY", default="lKwzkRnq8dksTthPauE61WrNPVwA3HdnJtQ9HuIyCpBib5zCY06tFsD5TUpNlCnO")
# https://docs.djangoproject.com/en/dev/ref/settings/#test-runner
TEST_RUNNER = "django.test.runner.DiscoverRunner"
# CACHES
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#caches
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.locmem.LocMemCache", "LOCATION": ""
}
}
# PASSWORDS
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#password-hashers
PASSWORD_HASHERS = ["django.contrib.auth.hashers.MD5PasswordHasher"]
# TEMPLATES
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#templates
TEMPLATES[0]["OPTIONS"]["debug"] = DEBUG # noqa F405
TEMPLATES[0]["OPTIONS"]["loaders"] = [ # noqa F405
(
"django.template.loaders.cached.Loader",
[
"django.template.loaders.filesystem.Loader",
"django.template.loaders.app_directories.Loader",
],
)
]
# EMAIL
# ------------------------------------------------------------------------------
# https://docs.djangoproject.com/en/dev/ref/settings/#email-backend
EMAIL_BACKEND = "django.core.mail.backends.locmem.EmailBackend"
# https://docs.djangoproject.com/en/dev/ref/settings/#email-host
EMAIL_HOST = "localhost"
# https://docs.djangoproject.com/en/dev/ref/settings/#email-port
EMAIL_PORT = 1025
# Your stuff...
# ------------------------------------------------------------------------------
Команда python manage.py test
сработала
Теперь я пишу новый тест с использованием pytest
import pytest
pytestmark = pytest.mark.django_db
@pytest.mark.django_db
def test_pytest():
assert True
Затем я запускаю pytest logical.path.to.pytests.test_pytest
Он говорит, что файл не может быть найден.
Я проверил base.py
и добавил logical
как приложение.
Где я go ошибся?