Fluent, chainable assertions for Django tests. Inspired by Laravel's elegant testing API.
Project description
pyssertive
Fluent, chainable assertions for Django tests. Inspired by Laravel's elegant testing API.
Features
- Fluent, chainable API for readable test assertions
- HTTP status code assertions (2xx, 3xx, 4xx, 5xx)
- JSON response validation with path navigation
- HTML content assertions
- Template and context assertions
- Form and formset error assertions
- Session and cookie assertions
- Header assertions
- Streaming response and file download assertions
- Debug helpers for test development
Requirements
- Python 3.11+
- Django 4.2+
Installation
pip install pyssertive
Usage
Basic Example
import pytest
from pyssertive.http import FluentHttpAssertClient
@pytest.fixture
def client():
from django.test import Client
return FluentHttpAssertClient(Client())
@pytest.mark.django_db
def test_user_api(client):
response = client.get("/api/users/")
response.assert_ok()\
.assert_json()\
.assert_json_path("count", 10)\
.assert_header("Content-Type", "application/json")
HTTP Status Assertions
response.assert_ok() # 2xx
response.assert_created() # 201
response.assert_not_found() # 404
response.assert_forbidden() # 403
response.assert_redirect("/login/")
response.assert_status(418) # Any status code
JSON Assertions
response.assert_json()\
.assert_json_path("user.name", "John")\
.assert_json_fragment({"status": "active"})\
.assert_json_count(5, path="items")\
.assert_json_structure({"id": int, "name": str})\
.assert_json_is_array()
Session and Cookie Assertions
response.assert_session_has("user_id", 123)\
.assert_session_missing("temp_token")\
.assert_cookie("session_id")\
.assert_cookie_missing("tracking")
Template Assertions
response.assert_template_used("users/list.html")\
.assert_context_has("users")\
.assert_context_equals("page", 1)
Streaming and Download Assertions
response.assert_streaming()\
.assert_download("report.csv")\
.assert_streaming_contains("Expected content")\
.assert_streaming_not_contains("Sensitive data")\
.assert_streaming_matches(r"ID:\d+")\
.assert_streaming_line_count(exact=10)\
.assert_streaming_line_count(min=5, max=20)\
.assert_streaming_csv_header(["id", "name", "email"])\
.assert_streaming_line(0, "header,row")\
.assert_streaming_empty()
Debug Helpers
response.dump() # Print full response
response.dump_json() # Pretty print JSON
response.dump_headers() # Print headers
response.dump_session() # Print session data
response.dd() # Dump and die (raises exception)
Database Assertions
from pyssertive.db import (
assert_model_exists,
assert_model_count,
assert_num_queries,
)
assert_model_exists(User, username="john")
assert_model_count(User, 5)
with assert_num_queries(2):
list(User.objects.all())
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyssertive-0.2.4.tar.gz.
File metadata
- Download URL: pyssertive-0.2.4.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e921c3dd5098aa7561c19018d3d90ea56a45333772f013f75f7d20cd2ff7be3e
|
|
| MD5 |
42948331f1bf91380526d0dd55c6c2d7
|
|
| BLAKE2b-256 |
c79303bdf2aa3a907f56be269468c69863d15d838ea7c1cf482cde91731e086d
|
Provenance
The following attestation bundles were made for pyssertive-0.2.4.tar.gz:
Publisher:
release.yml on othercodes/pyssertive
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyssertive-0.2.4.tar.gz -
Subject digest:
e921c3dd5098aa7561c19018d3d90ea56a45333772f013f75f7d20cd2ff7be3e - Sigstore transparency entry: 962125285
- Sigstore integration time:
-
Permalink:
othercodes/pyssertive@d6ac6bb194d4296207d9ab0b96863f6d4d570021 -
Branch / Tag:
refs/tags/v0.2.4 - Owner: https://github.com/othercodes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d6ac6bb194d4296207d9ab0b96863f6d4d570021 -
Trigger Event:
push
-
Statement type:
File details
Details for the file pyssertive-0.2.4-py3-none-any.whl.
File metadata
- Download URL: pyssertive-0.2.4-py3-none-any.whl
- Upload date:
- Size: 12.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3ca79ceeca41ab8ef53e5ad28ba116452b9426707b1cf36f11fb1c3f4b3434aa
|
|
| MD5 |
1128bafca02b3ead099d9297c4d4b448
|
|
| BLAKE2b-256 |
000f175df4b6ae6948cee4c77287ab836f325939bb75ac027e7d428e0340a1ed
|
Provenance
The following attestation bundles were made for pyssertive-0.2.4-py3-none-any.whl:
Publisher:
release.yml on othercodes/pyssertive
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pyssertive-0.2.4-py3-none-any.whl -
Subject digest:
3ca79ceeca41ab8ef53e5ad28ba116452b9426707b1cf36f11fb1c3f4b3434aa - Sigstore transparency entry: 962125298
- Sigstore integration time:
-
Permalink:
othercodes/pyssertive@d6ac6bb194d4296207d9ab0b96863f6d4d570021 -
Branch / Tag:
refs/tags/v0.2.4 - Owner: https://github.com/othercodes
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@d6ac6bb194d4296207d9ab0b96863f6d4d570021 -
Trigger Event:
push
-
Statement type: