A Django REST framework plugin for pytest.
Project description
pytest-drf
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%]
Project details
Release history Release notifications | RSS feed
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 pytest_drf-1.0.4-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fbab87c96d0a500a3de82c9960855ba065b54cdb9eabef9db1cca06e4fdc74d8 |
|
MD5 | 8fb4d289e89c74bd4d0dbf141e10c0c4 |
|
BLAKE2b-256 | ce868d0964b6161d0a4c32a97c82d2cd68c84723b6e7a854c1b47728c535ca53 |