0

For context, if I run pytest --ds="monolith.settings" or DJANGO_SETTINGS_MODULE="monolith.settings" pytest I see the expected behavior but currently I have a pytest.ini (see below) which I run as pytest -c pytest.ini which throws:

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

I've been trawling through different solutions such as using toml format, using envs, have followed the pytest-django instructions, and just about everything else I've found with no luck.

This is the envs that exist at runtime:

{'NVM_INC': '/Users/me/.nvm/versions/node/v16.13.0/include/node', 'rvm_use_flag': '', 'TERM_PROGRAM': 'iTerm.app', 'rvm_bin_path': '/Users/me/.rvm/bin', 'rvm_quiet_flag': '', 'NVM_CD_FLAGS': '-q', 'GEM_HOME': '/Users/me/.rvm/gems/ruby-3.0.1', 'TERM': 'xterm-256color', 'ASDF_DIR': '/Users/me/.asdf', 'rvm_gemstone_url': '', 'SHELL': '/usr/local/bin/zsh', 'MAKEFLAGS': '', 'TMPDIR': '/var/folders/7g/jdt1bt6n5zq7lykytwwqfdwc0000gn/T/', 'IRBRC': '/Users/me/.rvm/rubies/ruby-3.0.1/.irbrc', 'rvm_docs_type': '', 'TERM_PROGRAM_VERSION': '3.4.12', 'MY_RUBY_HOME': '/Users/me/.rvm/rubies/ruby-3.0.1', 'rvm_hook': '', 'TERM_SESSION_ID': 'w0t0p0:BD1C0A6B-9412-4E13-819C-D802B176C6EF', 'NVM_DIR': '/Users/me/.nvm', 'USER': 'me', 'rvm_gemstone_package_file': '', 'COMMAND_MODE': 'unix2003', 'rvm_path': '/Users/me/.rvm', 'SSH_AUTH_SOCK': '/private/tmp/com.apple.launchd.BcOmvUyUOZ/Listeners', '__CF_USER_TEXT_ENCODING': '0x1F5:0x0:0x0', 'MAKELEVEL': '1', 'rvm_proxy': '', 'VIRTUAL_ENV': '/Users/me/Library/Caches/pypoetry/virtualenvs/mono-RSwW7_xp-py3.10', 'rvm_ruby_file': '', 'MFLAGS': '', 'rvm_prefix': '/Users/me', 'rvm_silent_flag': '', 'PATH': '/Users/me/Library/Caches/pypoetry/virtualenvs/mono-RSwW7_xp-py3.10/bin:/Users/me/.rvm/gems/ruby-3.0.1/bin:/Users/me/.rvm/gems/ruby-3.0.1@global/bin:/Users/me/.rvm/rubies/ruby-3.0.1/bin:/Users/me/.nvm/versions/node/v16.13.0/bin:/Users/me/.pyenv/shims:/Users/me/.local/bin:/Users/me/.asdf/shims:/Users/me/.asdf/bin:/Users/me/.yarn/bin:/Users/me/.config/yarn/global/node_modules/.bin:/Users/me/.bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/MacGPG2/bin:/Users/me/.rvm/bin', 'rvm_ruby_make': '', '__CFBundleIdentifier': 'com.googlecode.iterm2', 'PWD': '/Users/me/code/labs/mono/services/core', 'POETRY_ACTIVE': '1', 'LANG': 'en_US.UTF-8', 'rvm_sdk': '', 'ITERM_PROFILE': 'me', 'XPC_FLAGS': '0x0', 'PS1': '(mono-py3.10) \\s-\\v\\$ ', 'XPC_SERVICE_NAME': '0', 'rvm_version': '1.29.12-next (master)', 'COLORFGBG': '7;0', 'rvm_script_name': '', 'SHLVL': '3', 'PYENV_SHELL': 'zsh', 'HOME': '/Users/me', 'rvm_pretty_print_flag': '', 'rvm_ruby_mode': '', 'LC_TERMINAL_VERSION': '3.4.12', 'ITERM_SESSION_ID': 'w0t0p0:BD1C0A6B-9412-4E13-819C-D802B176C6EF', 'LOGNAME': 'me', 'rvm_alias_expanded': '', 'GEM_PATH': '/Users/me/.rvm/gems/ruby-3.0.1:/Users/me/.rvm/gems/ruby-3.0.1@global', 'NVM_BIN': '/Users/me/.nvm/versions/node/v16.13.0/bin', 'rvm_nightly_flag': '', 'rvm_ruby_make_install': '', 'LC_TERMINAL': 'iTerm2', 'rvm_niceness': '', 'PLAT': 'macosx-11.1-x86_64', 'rvm_ruby_bits': '', 'rvm_bin_flag': '', 'SQLITE_EXEMPT_PATH_FROM_VNODE_GUARDS': '/Users/me/Library/WebKit/Databases', 'rvm_only_path_flag': '', 'RUBY_VERSION': 'ruby-3.0.1', 'COLORTERM': 'truecolor', '_': '/Users/me/Library/Caches/pypoetry/virtualenvs/mono-RSwW7_xp-py3.10/bin/pytest', 'PYTEST_CURRENT_TEST': 'domains/etl/ingress/tests/webhooks/test_webhook_mixin.py::WebhookTests::test_help (setup)'}

This is my pytest.ini:

[pytest]
env =
    ENV=test
    DJANGO_SETTINGS_MODULE = monolith.settings
addopts = -vv --disable-warnings
pythonpath = .

This is my pyproject.toml:

[tool.poetry]
name = ""
version = "0.1.1"
description = ""
authors = []

[tool.poetry.dependencies]
python = "^3.10"
Django = "^4.1.2"
celery = {extras = ["sqs"], version = "^5.2.7"}
pydantic = "^1.10.2"
psycopg2-binary = "^2.9.2"
uvicorn = {extras = ["standard"], version = "^0.15.0"}
gunicorn = "^20.1.0"
graphene = "^3.1.1"
graphene-pydantic = "^0.4.0"
djantic = "^0.7.0"
django-graphql-jwt = "^0.3.4"
django-admin-interface = "^0.20.0"

[tool.poetry.dev-dependencies]
pytest = "^7.2.0"
pytest-django = "^4.5.2"
black = "22.3.0"



[tool.black]
target_version = ['py36']

[build-system]
requires = ["poetry>=1.2.2"]
build-backend = "poetry.core.masonry.api"

This is my settings.py:

"""
Django settings for monolith project.

Generated by 'django-admin startproject' using Django 3.2.9.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""
import os
import json
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-s)1x24-1+d@9n0cg+jguy9zr@bx&n%!n_06@t9u^a1_ww6t3k="

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
CSRF_TRUSTED_ORIGINS = []  # json.loads(os.environ["CSRF_TRUSTED_ORIGINS"])
ALLOWED_HOSTS = ["*"]

AUTH_USER_MODEL = "administrator.User"


# Application definition

INSTALLED_APPS = [
    "admin_interface",
    "colorfield",
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    # Project Applications
    "administrator.apps.AdministratorConfig",
    "domains.etl.ingress.apps.IngressConfig",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
]

ROOT_URLCONF = "monolith.urls"

TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

WSGI_APPLICATION = "monolith.wsgi.application"


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.environ.get("POSTGRES_DB"),
        "USER": os.environ.get("POSTGRES_USER"),
        "PASSWORD": os.environ.get("POSTGRES_PASSWORD"),
        "HOST": os.environ.get("POSTGRES_HOST"),
        "PORT": 5432,
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
    },
    {
        "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

LANGUAGE_CODE = "en-us"

TIME_ZONE = "UTC"

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = "/static/"

MEDIA_URL = "/media/"
MEDIA_ROOT = os.path.join(BASE_DIR, "media")
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
Jango
  • 189
  • 1
  • 12
  • Looks like you are missing [pytest-env](https://pypi.org/project/pytest-env/) in your list of dependencies. – finswimmer Nov 28 '22 at 11:05

0 Answers0