Skip to main content

pytest-drf

PyPI version Build Status

pytest-drf is a pytest plugin for testing your Django REST Framework APIs.

pytest-drf aims to do away with clunky setup code and boilerplate for DRF testing, replacing it with declarative scaffolds that encourage small, easy-to-follow tests with single responsibilities.

This is accomplished by performing one request per test, and providing the response as a fixture. All configuration of the request — the URL, the query params, the HTTP method, the POST data, etc — is also done through fixtures. This leaves the test methods containing only assertions about the response or the state of the app after the request succeeds.

For example, let's consider a public API endpoint that responds with the JSON string "Hello, World!" and a 200 status code to a GET request. Such an endpoint might be written like so

# example/views.py

from rest_framework import permissions
from rest_framework.decorators import api_view, permission_classes
from rest_framework.response import Response

@api_view()
@permission_classes([permissions.AllowAny])
def hello_world(request):
    return Response('Hello, World!')

Let's route it to /hello, and give it a name, so we can easily generate URLs for it.

# example/urls.py

from django.urls import path

from example import views

urlpatterns = [
    path('hello', views.hello_world, name='hello-world'),
]

With pytest-drf, we'd verify the behavior of our endpoint with something like this

# tests/test_hello.py

class TestHelloWorld(
    APIViewTest,
    UsesGetMethod,
    Returns200,
):
    @pytest.fixture
    def url(self):
        return reverse('hello-world')

    def test_returns_hello_world(self, json):
        expected = 'Hello, World!'
        actual = json
        assert expected == actual

When we run pytest, we see two tests run

$ py.test

tests/test_hello.py::TestHelloWorld::test_it_returns_200 <- pytest_drf/status.py PASSED [ 50%]
tests/test_hello.py::TestHelloWorld::test_returns_hello_world PASSED                    [100%]

Metadata

Release files for pytest-drf 1.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-drf 1.0.4
File Size Uploaded Provenance Actions
pytest-drf-1.0.4.tar.gz 16.7 kB Without provenance Details for pytest-drf-1.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for pytest-drf 1.0.4
File Interpreter ABI Platform Actions
pytest_drf-1.0.4-py3-none-any.whl Python 3 none any Details for pytest_drf-1.0.4-py3-none-any.whl

Total release size: 28.9 kB

Release files / pytest-drf-1.0.4.tar.gz

Download URL pytest-drf-1.0.4.tar.gz
Size 16.7 kB
Tags Source
SHA-256 checksum
How to use checksums
9903ed3fc73d8a3f497a9bee17cca6f9c0cd78a9d048988d565ed6303618dfac
BLAKE2b-256 checksum
How to use checksums
ed443608e41f3b872623e08b78add6789b13e40aaf874d03ab0210af29620d6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.2 Linux/5.4.0-42-generic

Release files / pytest_drf-1.0.4-py3-none-any.whl

Download URL pytest_drf-1.0.4-py3-none-any.whl
Size 12.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fbab87c96d0a500a3de82c9960855ba065b54cdb9eabef9db1cca06e4fdc74d8
BLAKE2b-256 checksum
How to use checksums
ce868d0964b6161d0a4c32a97c82d2cd68c84723b6e7a854c1b47728c535ca53
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.0.10 CPython/3.8.2 Linux/5.4.0-42-generic

Security for pytest-drf 1.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page