Skip to main content

Lightweight PDF approval testing library.

Project description

pdf-approval

A lightweight Python library for approval (snapshot) testing of PDF outputs by comparing raw bytes.

Features

  • Binary Compare: Verifies exact byte-for-byte match between newly generated PDFs and their approved versions.
  • Easy Baseline Management: Stores .approved.pdf in source control, creates a .received.pdf on mismatch.
  • Auto-Approve: Allows updating baselines automatically if desired.
  • Cross-Platform: Works on Windows, macOS, and Linux.
  • Test Runner Agnostic: Raises AssertionError on mismatch (works with Pytest, Unittest, or any runner).

Installation

pip install pdf-approval

Quick Start

  1. Generate or obtain your PDF in bytes (e.g., from a report generator).
  2. Call verify_pdf(pdf_bytes, "my_report"). This looks for my_report.approved.pdf and compares it to the new PDF bytes.
  3. If no .approved.pdf is found or they differ, the library writes a .received.pdf file and raises an error:
    • Inspect the new PDF to ensure it’s correct.
    • Approve by renaming it or calling approve_received("my_report").

Example

from pdf_approval.approval_util import verify_pdf, approve_received

def test_generate_monthly_report():
    # Suppose we create some PDF data
    pdf_data = create_monthly_report()  # returns raw PDF bytes
    
    # Compare to an approved file named 'monthly_report.approved.pdf'
    verify_pdf(pdf_data, "monthly_report")

    # If mismatch => test fails, a 'monthly_report.received.pdf' is produced.
    # If it doesn't exist => also fails, instructing you to approve.
    # To approve changes (after checking them):
    # approve_received("monthly_report")

Auto-Approve

If you want the library to automatically update the .approved.pdf file when a mismatch is detected (bypassing the test failure), set an environment variable or use the library function:

export APPROVAL_AUTO_APPROVE=1 pytest

Or in code:

from pdf_approval.approval_util import set_auto_approve
set_auto_approve(True)

Use with caution – always inspect changes to ensure they’re valid.

Best Practices

  1. Commit Approved Files: Check *.approved.pdf into version control.
  2. Ignore Received Files: Add *.received.pdf to your .gitignore.
  3. Review Mismatches: Don’t just blindly approve – open a diff or PDF viewer to confirm changes are valid.
  4. Stable PDF Generation: Remove timestamps, IDs, or other variable data from your PDF if possible to avoid spurious diffs.
  5. Auto-Approve in CI? Usually not recommended; you want test failures for unexpected changes.

Contributing

  1. Fork or clone this repository.
  2. Make changes in a dedicated branch.
  3. Add or update tests as needed.
  4. Submit a pull request.

All contributions, big or small, are welcome!

License

MIT License

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

pdf_approval-0.1.2.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

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

pdf_approval-0.1.2-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file pdf_approval-0.1.2.tar.gz.

File metadata

  • Download URL: pdf_approval-0.1.2.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for pdf_approval-0.1.2.tar.gz
Algorithm Hash digest
SHA256 5bc0bd1fc284ca0350dcf030e742d765931a6de8db741c38ace39afbc35300ec
MD5 795ee90f91deebe06913cfd4785a2f7a
BLAKE2b-256 036c1574e11e685261616a74a874dddffd4bfa5cb915ea2e985a3921cbbdc31f

See more details on using hashes here.

File details

Details for the file pdf_approval-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: pdf_approval-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.6

File hashes

Hashes for pdf_approval-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 35ed19802b6b6395d44b798f3dc7739bfadf292e4a94d863710bb4a094edc2b0
MD5 da643c6d0eedabe73f6cc2b6e7c59c3a
BLAKE2b-256 647fd5ed799435d60755fba16b78809462666aa8fa2b1eaf6130c02fa635609e

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