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.pdfin source control, creates a.received.pdfon mismatch. - Auto-Approve: Allows updating baselines automatically if desired.
- Cross-Platform: Works on Windows, macOS, and Linux.
- Test Runner Agnostic: Raises
AssertionErroron mismatch (works with Pytest, Unittest, or any runner).
Installation
pip install pdf-approval
Quick Start
- Generate or obtain your PDF in bytes (e.g., from a report generator).
- Call
verify_pdf(pdf_bytes, "my_report"). This looks formy_report.approved.pdfand compares it to the new PDF bytes. - If no
.approved.pdfis found or they differ, the library writes a.received.pdffile 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
- Commit Approved Files: Check
*.approved.pdfinto version control. - Ignore Received Files: Add
*.received.pdfto your.gitignore. - Review Mismatches: Don’t just blindly approve – open a diff or PDF viewer to confirm changes are valid.
- Stable PDF Generation: Remove timestamps, IDs, or other variable data from your PDF if possible to avoid spurious diffs.
- Auto-Approve in CI? Usually not recommended; you want test failures for unexpected changes.
Contributing
- Fork or clone this repository.
- Make changes in a dedicated branch.
- Add or update tests as needed.
- Submit a pull request.
All contributions, big or small, are welcome!
License
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 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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d8ecfc2d333b45b8c998639be8cd965f5e5aca0c71b94029430d29db9fb12d3
|
|
| MD5 |
da0c098f1421628a1c07196e1786b10b
|
|
| BLAKE2b-256 |
08b3486a24f7175d1be6892203218a428b4f26a3b59834197734ddc29aeac68e
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf2166fcf4907ebdec3bba407104ceefb40f45c10dcce16fd57a58dfe9460d7a
|
|
| MD5 |
ca21d8d566fcb8c6d68ba6d8dc3635a3
|
|
| BLAKE2b-256 |
ee8e5da6e182f17186087348fc7d7734bca4446d5bd90c61d1ba1f1435fa272e
|