The drf-api-action Python package is designed to elevate your testing experience for Django Rest Framework (DRF) REST endpoints. With the api_action fixture, this package empowers you to effortlessly test your REST endpoints as if they were conventional functions.
Features:
-
Simplified Testing: Testing DRF REST endpoints using the api_action plugin, treating them like regular functions.
-
Seamless Integration: you don't need to do anything in existing server code.
-
Easy Debugging: Instead of getting a response with error code by using default drf testing
clientobject , get the real traceback that led to the error. -
Pagination Support: Paginating easily through pages by using
pageargument.
Installation
You can install drf-api-action using pip:
pip install drf-api-action
Usage
To use drf-api-action as a Pytest fixture, you need to follow these steps:
Step 1: Import your Viewsets explicitly:
import pytest
from tests.test_server.test_app.models import DummyModel
from tests.test_server.test_app.views import DummyViewSetFixture
Step 2: use the following action_api mark decorator:
@pytest.mark.api_action(view_set_class={YOUR VIEW_SET})
e.g:
our ViewSet is called DummyViewSetFixture
import pytest
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.api_action(view_set_class=DummyViewSetFixture)
def test_call_as_api_fixture(db, api_action):
pass
Now you can use all DummyViewSetFixture functionality!
Step 3: write your tests
e.g:
our ViewSet is called DummyViewSetFixture
import pytest
from tests.test_server.test_app.models import DummyModel
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.api_action(view_set_class=DummyViewSetFixture)
def test_call_as_api_fixture(db, api_action):
dummy_model = DummyModel()
dummy_model.dummy_int = 1
dummy_model.save()
res = api_action.api_dummy(pk=1)
assert res["dummy_int"] == 1
Here as an example, the real exception and trace will be thrown, make it easy to understand what the issue is:
import pytest
from tests.test_server.test_app.views import DummyViewSetFixture
@pytest.mark.api_action(view_set_class=DummyViewSetFixture)
def test_dummy(db, api_action):
result = api_action.dummy(pk='bbb')
assert result['dummy_int'] == 1
tests/functionality_tests/test_as_api.py:11 (test_call_as_api)
self = <django.db.models.fields.BigAutoField: id>, value = 'bb'
def get_prep_value(self, value):
value = super().get_prep_value(value)
if value is None:
return None
try:
> return int(value)
E ValueError: invalid literal for int() with base 10: 'bb'
../venv/lib/python3.9/site-packages/django/db/models/fields/__init__.py:2053: ValueError
The above exception was the direct cause of the following exception:
queryset = <QuerySet [<DummyModel: DummyModel object (1)>]>, filter_args = ()
filter_kwargs = {'pk': 'bb'}
def get_object_or_404(queryset, *filter_args, **filter_kwargs):
"""
Same as Django's standard shortcut, but make sure to also raise 404
if the filter_kwargs don't match the required types.
"""
try:
> return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
../venv/lib/python3.9/site-packages/rest_framework/generics.py:19:
....
....
...
Call endpoints with pagination:
@pytest.mark.api_action(view_set_class=DummyAPIViewSet)
def test_pagination_data(db, api_action):
for i in range(1, 3):
dummy_model = DummyModel()
dummy_model.dummy_int = 1
dummy_model.save()
response = api_action.by_dummy_int(dummy_int=1, page=1)
obj = response['results'][0]
assert obj['dummy_int'] == 1
assert extract_page_number(response['next']) == 2
response = api_action.by_dummy_int(dummy_int=1, page=2)
assert extract_page_number(response['previous']) == 1
assert extract_page_number(response['next']) is None
Package Testing
The drf-api-action library includes tests to ensure the functionality works as expected. To run the tests run pytest:
pytest
The tests will be executed, and the results will be displayed in the console.
Example
Example of using drf-api-action in a DRF project
Support & Contribution
For guidance on support & contribution, see the contributing guidelines.
Bug Report
For guidance on how to open a bug, see the bug report template.
Open an Issue
For guidance on how to open an issue, see the issue template.
Code Of Conduct
Metadata
Release files for drf-api-action 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| drf_api_action-1.2.2.tar.gz | 6.6 kB | Without provenance | Details for drf_api_action-1.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| drf_api_action-1.2.2-py3-none-any.whl | Python 3 | none | any | Details for drf_api_action-1.2.2-py3-none-any.whl |
Total release size: 12.7 kB
Release files / drf_api_action-1.2.2.tar.gz
| Download URL | drf_api_action-1.2.2.tar.gz |
|---|---|
| Size | 6.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
c928c935ff40a0a0401232d3e6363db0c505b2e24c11173de7deb3a9be401c65
|
|
BLAKE2b-256 checksum How to use checksums |
13485ef3f92d8ccf8ac9b1e0c094466f52744a6cca6f68e76ddd337bf6415355
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Release files / drf_api_action-1.2.2-py3-none-any.whl
| Download URL | drf_api_action-1.2.2-py3-none-any.whl |
|---|---|
| Size | 6.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3630ff2bdc537815a522534c94ebbef878e0e738a84c1e8c9640b4e1f79b96ee
|
|
BLAKE2b-256 checksum How to use checksums |
e9fdb56f8a6a5de37d7508c09bc67cff6182d6d93664f4e447b4164ee7ec95cb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.9.12
|
Security for drf-api-action 1.2.2
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.