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

Uploaded Python 3

File details

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

File metadata

  • Download URL: pdf_approval-0.1.1.tar.gz
  • Upload date:
  • Size: 6.1 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.1.tar.gz
Algorithm Hash digest
SHA256 2d8ecfc2d333b45b8c998639be8cd965f5e5aca0c71b94029430d29db9fb12d3
MD5 da0c098f1421628a1c07196e1786b10b
BLAKE2b-256 08b3486a24f7175d1be6892203218a428b4f26a3b59834197734ddc29aeac68e

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pdf_approval-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 bf2166fcf4907ebdec3bba407104ceefb40f45c10dcce16fd57a58dfe9460d7a
MD5 ca21d8d566fcb8c6d68ba6d8dc3635a3
BLAKE2b-256 ee8e5da6e182f17186087348fc7d7734bca4446d5bd90c61d1ba1f1435fa272e

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