Skip to content

Commit

Permalink
Add setting example using pyproject.toml file. (#1047)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernardoow committed Apr 5, 2023
1 parent 90349e5 commit 5337357
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
17 changes: 15 additions & 2 deletions docs/configuring_django.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ Example contents of pytest.ini::
[pytest]
DJANGO_SETTINGS_MODULE = test.settings

``pyproject.toml`` settings
---------------------------

Example contents of pyproject.toml::

[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "test.settings"

Order of choosing settings
--------------------------

Expand All @@ -46,7 +54,7 @@ The order of precedence is, from highest to lowest:
* The command line option ``--ds``
* The environment variable ``DJANGO_SETTINGS_MODULE``
* The ``DJANGO_SETTINGS_MODULE`` option in the configuration file -
``pytest.ini``, or other file that Pytest finds such as ``tox.ini``
``pytest.ini``, or other file that Pytest finds such as ``tox.ini`` or ``pyproject.toml``

If you want to use the highest precedence in the configuration file, you can
use ``addopts = --ds=yourtestsettings``.
Expand All @@ -57,7 +65,7 @@ Using django-configurations
There is support for using `django-configurations <https://pypi.python.org/pypi/django-configurations/>`_.

To do so configure the settings class using an environment variable, the
``--dc`` flag, or ``pytest.ini`` option ``DJANGO_CONFIGURATION``.
``--dc`` flag, ``pytest.ini`` option ``DJANGO_CONFIGURATION`` or ``pyproject.toml`` option ``DJANGO_CONFIGURATION``.

Environment Variable::

Expand All @@ -73,6 +81,11 @@ INI File Contents::
[pytest]
DJANGO_CONFIGURATION=MySettings

pyproject.toml File Contents::

[tool.pytest.ini_options]
DJANGO_CONFIGURATION = "MySettings"

Using ``django.conf.settings.configure()``
------------------------------------------

Expand Down
14 changes: 14 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Make sure ``DJANGO_SETTINGS_MODULE`` is defined (see
:ref:`configuring_django_settings`) and make your tests discoverable
(see :ref:`faq-tests-not-being-picked-up`):

Example using pytest.ini or tox.ini
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: ini
# -- FILE: pytest.ini (or tox.ini)
Expand All @@ -27,6 +30,17 @@ Make sure ``DJANGO_SETTINGS_MODULE`` is defined (see
# -- recommended but optional:
python_files = tests.py test_*.py *_tests.py
Example using pyproject.toml
~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. code-block:: toml
# -- Example FILE: pyproject.toml
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "test.settings"
# -- recommended but optional:
python_files = ["test_*.py", "*_test.py", "testing/python/*.py"]
Run your tests with ``pytest``:

.. code-block:: bash
Expand Down
7 changes: 7 additions & 0 deletions docs/tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ contains:
[pytest]
DJANGO_SETTINGS_MODULE = yourproject.settings
Another options for people that use ``pyproject.toml`` is add the following code:

.. code-block:: toml
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "yourproject.settings"
You can also specify your Django settings by setting the
``DJANGO_SETTINGS_MODULE`` environment variable or specifying the
``--ds=yourproject.settings`` command line flag when running the tests.
Expand Down

0 comments on commit 5337357

Please sign in to comment.