pytest-django

Description

pytest-django is a plugin for pytest that provides a set of useful tools for testing Django applications and projects.

conftest.py example

We want to test a staging database

 1 """conftest.py
 2
 3
 4 For information, we could also use this code::
 5
 6     os.environ[
 7         "DATABASE_URL"
 8     ] = "postgresql://<username>:<password>@W.X.Y.Z:5432/db_XXXX"
 9     DATABASES = {"default": env.db("DATABASE_URL")}
10
11
12     # on corrige par anticipation car django-environ fait pointer sur
13     # 'django.db.backends.postgresql_psycopg2' au lieu de 'django.db.backends.postgresql'
14     # A ce jour (2017-12-01 les 2 modules existent.
15     # https://docs.docker.com/compose/django/#connect-the-database
16
17     DATABASES["default"]["ENGINE"] = "django.db.backends.postgresql"
18
19
20 Calling pytest::
21
22     pytest --cov='.'
23
24 """
25
26 import pytest
27 from django.conf import settings
28
29
30 @pytest.fixture(scope="session")
31 def django_db_setup():
32
33     settings.DATABASES["default"] = {
34         "ENGINE": "django.db.backends.postgresql",
35         # Test database
36         "HOST": "W.X.Y.Z",
37         "NAME": "db_XXXX",
38     }