Skip to main content

pytest-sanitizer - Pytest Output Sanitizer for LLMs

A pytest plugin that sanitizes test output to prevent LLMs from seeing expected values in assertion errors.

DISCLAIMER

USE AT YOUR OWN RISK: This tool is provided "AS IS" without any warranties or guarantees of any kind, express or implied. The author takes NO RESPONSIBILITY and accepts NO LIABILITY for any issues, failures, or damages that may arise from using this tool.

NO WARRANTY: There is absolutely no guarantee that this tool will work correctly or continue to work in the future. It may fail to sanitize certain outputs, miss assertion errors, or interfere with your test runs in unexpected ways.

NO SUPPORT: This is a personal tool not intended for production use. Issues, bugs, or compatibility problems will likely not be addressed.

If you use this tool and miss failing tests or if it causes any other problems in your testing process, that's entirely your responsibility.

Maintenance Status

Personal Tool: This package is primarily developed for personal use and not actively maintained for the broader community. Feel free to use it, but be aware that issues and pull requests might not be addressed.

Problem

LLMs sometimes "cheat" by looking at the expected values in assertion errors. For example, given an error like:

FAILED tests/test_utils.py::test_format_duration - AssertionError: assert '1d' == '1d 2h'

An LLM might modify the test to match the result given by the code, or worse, hardcode the expected result.

Solution

pytest-sanitizer hooks into pytest's reporting system and sanitizes the output, removing the expected values from assertion errors while maintaining:

  • Error tracebacks
  • Colorized output
  • Enough context to debug the issue

Installation

For local installation from GitHub:

# Clone the repository
git clone https://github.com/username/pytest-sanitizer.git

# Install in development mode
cd pytest-sanitizer
pip install -e .

Or install from PyPI:

pip install pytest-sanitizer

Usage

After installation, the plugin is automatically enabled when running pytest:

pytest tests/

The output will be sanitized to hide expected values in assertion errors.

You can also use the provided command-line tool:

pytest-sanitizer tests/

Disabling Sanitization

For debugging or when you need to see the actual expected values, you can disable sanitization:

# Using pytest directly
pytest tests/ --no-sanitize  # or the shorter --ns flag

# Using the command-line tool
pytest-sanitizer tests/ --no-sanitize  # or --ns

Example

Original pytest output:

FAILED tests/test_utils.py::test_format_duration - AssertionError: assert '1d' == '1d 2h'

Sanitized output:

FAILED tests/test_utils.py::test_format_duration - AssertionError: assert '1d' == [SANITIZED]

[NOTE: This output has been sanitized to prevent revealing expected values. You need to determine whether the issue is in the test or the implementation.]

Demo

Run the included demo script to see how the sanitizer transforms assertion messages:

python demo.py

License

MIT License

Metadata

Release files for pytest-sanitizer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-sanitizer 0.1.0
File Size Uploaded
pytest_sanitizer-0.1.0.tar.gz 11.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-sanitizer 0.1.0
File Interpreter ABI Platform
pytest_sanitizer-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 25.3 kB

Release files / pytest_sanitizer-0.1.0.tar.gz

Download URL pytest_sanitizer-0.1.0.tar.gz
Size 11.1 kB
Tags Source
SHA-256 checksum
How to use checksums
a2067096d64c71ce94b0d913558d72f497f420b646a177cd1829e8ab05654f56
BLAKE2b-256 checksum
How to use checksums
8f8987410146d77883ff7948db4da383eaee5ed68174cd833827455c43442ff4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release files / pytest_sanitizer-0.1.0-py3-none-any.whl

Download URL pytest_sanitizer-0.1.0-py3-none-any.whl
Size 14.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6268057736796e4e3c5d1d3767c6212b5ca9b7bac89cbb6a1575cd9d40d6c7d7
BLAKE2b-256 checksum
How to use checksums
3a01e766e8358382f4d9901ac2e9e036e495ce76d54e11e8c55ac5c7b4f3fa8a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.11

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page