Run Django tests with testcontainers.
Project description
django-rdtwt
(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
-
Add rdtwt to your INSTALLED_APPS setting like this
INSTALLED_APPS = [ ... 'rdtwt', ]
-
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
Built Distribution
Hashes for django_rdtwt-2.0.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 249f7a60bdf6926aac8a8468776231b91867dee3f94e7dd478e0e26d37b7e9f2 |
|
MD5 | bd30be4a0c4d5c9015d50d4d99d047a2 |
|
BLAKE2b-256 | f7cadda8c57fc91f971f016e24385108d4dd4f4db58245375c1048b350ace5c7 |