Skip to main content

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

Project description

pyssertive

Build Status

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
  • 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)

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())

License

Apache License 2.0

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.1.0.tar.gz (14.9 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.1.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pyssertive-0.1.0.tar.gz
  • Upload date:
  • Size: 14.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyssertive-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fb28d4bc1c00e4a3173181631e5f3e3768ebb192175452a31bdcc5124d7aa933
MD5 a89ad58c44ab8340f43e34bc7f64d40e
BLAKE2b-256 efecd374fc9ea98a747f65b081012d5e69cc90c20577d4cedcf90b03e3eb6682

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pyssertive-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for pyssertive-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0074b82af6207e62f792bba14ac5391acb35ca9a85f22f18afeec8bcfb4057ac
MD5 af54a84c0b2a022a3773bda403522098
BLAKE2b-256 07764a67948824cd5dd222c0553382a15b775fab4057ccc1bd0a9ad01e120445

See more details on using hashes here.

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