Skip to main content

Run Django tests with testcontainers.

Project description

django-rdtwt

Ruff Tests Code style: black PyPI

(Run Django Tests With Testcontainers)

This focuses on users who wish to forget setting up a database for tests. There's no need for manually starting up Docker compose or local database with this.

Note:

See: Altering settings at runtime: Django documentation

By default, this test runner changes default database host and port dynamically, because it's quite hard to know database host beforehand in dynamic environments, for example, some CI/CD runners.

Still, this just works and is quite simple, that's why I haven't spent that much time investigating alternative solutions.

Installation

  1. Add rdtwt to your INSTALLED_APPS setting like this

    INSTALLED_APPS = [
        ...
        'rdtwt',
    ]
    
  2. Run tests with rdtwt runner,

    python manage.py test --testrunner=rdtwt.runner.PostgresDiscoverRunner
    

Though what I really suggest is to put the following in your test settings.

# RDTWT SETTINGS
RDTWT_POSTGRESQL_IMAGE = 'postgres:14.1'
TEST_RUNNER = 'rdtwt.runner.PostgresDiscoverRunner'

This makes sure that tests run against the PostgreSQL version defined by you. It also adds up to the test confidence; at least they aren't flaky because of a database version changing without your knowledge.

Example:

Defining all available options.

# Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': '127.0.0.1',
        'PORT': '5432'
    }
}

# RDTWT SETTINGS
RDTWT_POSTGRESQL_IMAGE = 'postgres:14.1'
TEST_RUNNER = 'rdtwt.runner.PostgresDiscoverRunner'
RDTWT_DATABASES = ["default"]  # Add more if you have multiple databases
RDTWT_POSTGRESQL_USER = 'postgres'
RDTWT_POSTGRESQL_PASSWORD = 'postgres'
RDTWT_POSTGRESQL_NAME = 'postgres'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_rdtwt-3.0.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_rdtwt-3.0.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file django_rdtwt-3.0.0.tar.gz.

File metadata

  • Download URL: django_rdtwt-3.0.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for django_rdtwt-3.0.0.tar.gz
Algorithm Hash digest
SHA256 b812416ee3c8f6efd071d87f943ac10dc25a16e49c4b5b6b1f367cb091d72983
MD5 e459f764825528fad23f75bb257e353e
BLAKE2b-256 cb8c2ee95bdf82ce482fe9d35fc6bb17f6fa631c7fd125275e3bd31b854080a7

See more details on using hashes here.

File details

Details for the file django_rdtwt-3.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_rdtwt-3.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for django_rdtwt-3.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 192b39ee4647f5026e2c2ff04ac515d5ffe188e600f979d2d95db3cca3de90a1
MD5 95d75ee461f977ba8f1fd8aeaf8df566
BLAKE2b-256 57ad58f9bab3a88a79412f7d7e6fbbcf0ac3e5713e27f85982cd3fff818ba16b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page