protest-insta
Property-based snapshot testing integration for Protest and Insta.
Overview
protest-insta combines the power of property-based testing with snapshot testing, allowing you to:
- Test with diverse inputs while maintaining visual regression testing
- Detect unexpected changes in serialization, formatting, or computation results
- Document behavior through automatically captured snapshots
- Review changes using Insta's powerful review workflow
Quick Start
Add to your Cargo.toml:
[]
= "0.1"
= "0.1"
= "1.41"
= { = "1.0", = ["derive"] }
Basic Example
use ;
use PropertySnapshots;
use Serialize;
use SeedableRng;
use StdRng;
Features
PropertySnapshots Helper
The PropertySnapshots struct manages multiple snapshots with automatic sequential naming:
use PropertySnapshots;
let mut snapshots = new;
// Creates snapshots named: my_test_0, my_test_1, my_test_2, ...
snapshots.assert_json_snapshot;
snapshots.assert_json_snapshot;
snapshots.assert_debug_snapshot;
Supported Snapshot Types
JSON Snapshots
Perfect for testing serialization of structured data:
use Serialize;
let config = Config ;
snapshots.assert_json_snapshot;
Debug Snapshots
Great for testing computation results and non-serializable types:
let results: = vec!;
snapshots.assert_debug_snapshot;
YAML Snapshots
For YAML-formatted snapshots:
use Serialize;
let settings = Settings ;
snapshots.assert_yaml_snapshot;
Helper Function
The property_snapshot_test function provides a concise API:
use IntGenerator;
use property_snapshot_test;
use Serialize;
Use Cases
1. Serialization Testing
Test that your types serialize consistently across different inputs:
use VecGenerator;
use Serialize;
2. API Response Testing
Verify API responses remain stable:
3. Computation Result Testing
Document computation behavior across inputs:
4. Visual Regression Testing
Detect unexpected changes in output format:
Best Practices
1. Use Deterministic Generators
Always use a seeded RNG for reproducible snapshots:
use SeedableRng;
use StdRng;
let mut rng = seed_from_u64; // ✅ Reproducible
// let mut rng = rand::thread_rng(); // ❌ Non-deterministic
2. Limit Sample Count
Keep snapshot counts reasonable (5-10) to make reviews manageable:
let mut snapshots = new;
for _ in 0..5
// for _ in 0..1000 { // ❌ Too many snapshots
3. Use Descriptive Base Names
Choose clear, descriptive names for snapshot groups:
new // ✅ Clear
new // ❌ Unclear
4. Group Related Snapshots
Use the same base name for related test scenarios:
let mut snapshots = new;
snapshots.assert_debug_snapshot;
snapshots.assert_debug_snapshot;
snapshots.assert_debug_snapshot;
5. Review Snapshots Regularly
Use Insta's review workflow:
# Review all pending snapshots
# Accept all snapshots
# Reject all snapshots
Comparison with Traditional Testing
Traditional Snapshot Testing
Limitations:
- Tests only one fixed input
- May miss edge cases
- Requires manual input selection
Property-Based Snapshot Testing
Benefits:
- Tests multiple diverse inputs automatically
- Discovers edge cases
- Better coverage with less code
Examples
See the examples/ directory for complete working examples:
json_snapshots.rs- JSON snapshot testing with complex structuresdebug_snapshots.rs- Debug snapshots for computation resultsproperty_snapshot_test.rs- Using the helper function
Run examples with:
API Reference
PropertySnapshots
Manages a group of related snapshots with automatic naming.
Methods
new(base_name)- Create a new snapshot helperassert_json_snapshot(&value)- Create a JSON snapshotassert_debug_snapshot(&value)- Create a debug snapshotassert_yaml_snapshot(&value)- Create a YAML snapshotreset()- Reset the counter to 0count()- Get the current counter value
property_snapshot_test
Helper function for concise property-based snapshot testing.
Parameters
test_name: &str- Base name for snapshotsgenerator: G- Generator for test inputssample_count: usize- Number of samples to generateseed: u64- RNG seed for reproducibilitytest_fn: F- Test function receiving each generated value
Integration with Insta
This crate is built on top of Insta, so all Insta features work seamlessly:
- Snapshot review workflow -
cargo insta review - Inline snapshots - Use Insta's inline snapshot macros
- Settings - Configure Insta with
insta::Settings - Filters - Apply Insta's redaction filters
FAQ
Q: How do I review snapshots?
A: Use Insta's CLI tool:
Q: Where are snapshots stored?
A: By default, in a snapshots/ directory next to your test file. Insta manages this automatically.
Q: Should I commit snapshots to git?
A: Yes! Snapshots are part of your test suite and should be version controlled.
Q: How many samples should I generate?
A: Start with 5-10. More samples give better coverage but make reviews more tedious.
Q: Can I use this with existing Insta tests?
A: Absolutely! protest-insta is just a helper layer on top of Insta. Mix and match freely.
Contributing
Contributions are welcome! Please see the main Protest repository for contribution guidelines.
License
Licensed under the MIT license. See LICENSE for details.