Skip to main content

Run Django tests with testcontainers.

Project description

django-rdtwt

Static analysis 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-2.1.0.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file django-rdtwt-2.1.0.tar.gz.

File metadata

  • Download URL: django-rdtwt-2.1.0.tar.gz
  • Upload date:
  • Size: 4.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for django-rdtwt-2.1.0.tar.gz
Algorithm Hash digest
SHA256 2eadaa71f4fd703dfcdab7052cb37d9ee1755fe9e9cbd17dc99d7a9b91a46261
MD5 e883afb66f77cffc887877c36ce440d5
BLAKE2b-256 aebb80ce66d9250d8f875ce35d2de427a77302a91119eb19c949142d325fed11

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for django_rdtwt-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c5104540158547d9fe272d28fd7de94e40a64ab8b6bc1ad6400212691fd1d92
MD5 8fc46491f3967e432d0314fd68c7d7f4
BLAKE2b-256 c79edc6a5020fe2de0c9ea91d128726eeb3150b5da3a58e627151394b2dd16bf

See more details on using hashes here.

Supported by

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