Metadata-Version: 2.4
Name: safetydance-django
Version: 0.1.0
Summary: safetydance steps for BDD testing of Django REST Framework applications.
Author: David Charboneau
Author-email: David Charboneau <david@adadabase.com>
License-Expression: MIT
License-File: AUTHORS.rst
License-File: LICENSE.txt
Classifier: Development Status :: 4 - Beta
Classifier: Framework :: Django
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Topic :: Software Development :: Testing :: BDD
Requires-Dist: safetydance>=0.5,<0.6
Requires-Dist: django>=4.2
Requires-Dist: djangorestframework>=3.15
Requires-Python: >=3.10
Project-URL: Documentation, https://dcharbon.gitlab.io/safetydance_django
Project-URL: Repository, https://gitlab.com/dcharbon/safetydance_django.git
Description-Content-Type: text/x-rst

==================
safetydance_django
==================

Django and Django REST framework test steps for `safetydance
<https://pypi.org/project/safetydance/>`_. Drive an ``APIClient`` with
``Given.http.get(...)``, ``When.http.post(...)`` and check the response with
``Then.http.status_code_is(...)``.

Install
=======

.. code-block:: bash

    pip install safetydance-django

This needs Python 3.10 or later and safetydance 0.5.x. The example below also
needs ``pytest`` and ``pytest-django``, which are not installed as dependencies
of safetydance-django.

Example
=======

.. code-block:: python

    from django.urls import reverse
    import pytest
    from safetydance_django.test import *


    @pytest.mark.django_db
    @test_context
    def test_get():
        Given.http.get(reverse("simple-list"))
        Then.http.status_code_is(200)
        assert Http.response.status_code == 200

``reverse("simple-list")`` refers to the example app in this repository's
tests. Substitute the URL name of one of your own views.

The steps are registered at ``Given.http.*`` (``get``, ``post``, ``put``,
``delete``, ``options``, ``head``, ``login``, ``defaults`` and so on) when
``safetydance_django.test`` or ``safetydance_django.steps`` is imported. Import
one of them in ``conftest.py``, or star-import ``safetydance_django.test`` as
above. The steps have no bare names: ``Given.get`` raises ``StepNotFound``.

``safetydance_django.test`` exports ``And``, ``Given``, ``Http``, ``Then``,
``When`` and ``test_context``. Put ``@test_context`` below any
``@pytest.mark.*`` decorators and above any ``@mock.patch`` decorators.
``Http.api_client`` (an ``APIClient``) and ``Http.response`` hold the HTTP
state of the current test. The client is not called ``client`` because
``@test_context`` stores pytest fixtures by name, and pytest-django's
``client`` fixture would replace it.

Step modules of your own register their steps only when they are imported, so
import them in ``conftest.py``. A plain ``@step`` registers a bare name, and a
conflict with an existing registration raises when you register.

Documentation
=============

Documentation on the project is at https://dcharbon.gitlab.io/safetydance_django.
