Skip to main content

Fluent, chainable assertions for Django tests. Inspired by Laravel's elegant testing API.

Project description

pyssertive

Build Status Coverage

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.10+
  • Django 4.2, 5.2, or 6.0

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyssertive-0.3.1.tar.gz (22.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyssertive-0.3.1-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file pyssertive-0.3.1.tar.gz.

File metadata

  • Download URL: pyssertive-0.3.1.tar.gz
  • Upload date:
  • Size: 22.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyssertive-0.3.1.tar.gz
Algorithm Hash digest
SHA256 8c7620a326322f717e0ded1295be7b99275dfc7e3a88b1a6144e758787bec055
MD5 692b58c51a78e1b7b7c25a9b1e7edbaa
BLAKE2b-256 28e22ef57ac2484f5f2ed075c95b70b203fd83fe70c093994b38761ad82e95ad

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyssertive-0.3.1.tar.gz:

Publisher: release.yml on othercodes/pyssertive

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyssertive-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyssertive-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyssertive-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3f291271ad8d522792ab2f13bf4f29eb01398c2738647b9c6ecc00529749421a
MD5 c4e62af0a2b049207eb70a3d94a603d0
BLAKE2b-256 26bb2e67da047b01b7b92240247460afe4a7f2c93de1d08938b938aeead7d4e4

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyssertive-0.3.1-py3-none-any.whl:

Publisher: release.yml on othercodes/pyssertive

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page