Expand description
Property-Based Snapshot Testing with Insta
This crate provides integration between Protest property-based testing and Insta snapshot testing.
§Overview
Property-based snapshot testing allows you to:
- Test with diverse, generated inputs while keeping visual regression testing
- Detect unexpected changes in serialization output
- Document behavior through automatically captured snapshots
- Review changes using Insta’s review workflow
§Quick Start
use protest::{Generator, primitives::IntGenerator, config::GeneratorConfig};
use protest_insta::PropertySnapshots;
use serde::Serialize;
use rand::SeedableRng;
use rand::rngs::StdRng;
#[derive(Serialize)]
struct Point { x: i32, y: i32 }
#[test]
fn test_point_serialization() {
let mut rng = StdRng::seed_from_u64(42);
let config = GeneratorConfig::default();
let generator = IntGenerator::new(0, 100);
let mut snapshots = PropertySnapshots::new("point_serialization");
for _ in 0..5 {
let x = generator.generate(&mut rng, &config);
let y = generator.generate(&mut rng, &config);
let point = Point { x, y };
snapshots.assert_json_snapshot(&point);
}
}§Features
- Generator Integration: Use any Protest generator for snapshot inputs
- Automatic Naming: Sequential snapshot names for property-based tests
- JSON Support: Built-in JSON serialization for snapshots
- Debug Support: Snapshot any Debug type
- Insta Workflow: Full compatibility with Insta’s review tools
§Examples
§JSON Snapshot Testing
use protest::{Generator, primitives::VecGenerator, primitives::IntGenerator};
use protest::{config::GeneratorConfig};
use protest_insta::PropertySnapshots;
use serde::Serialize;
use rand::SeedableRng;
use rand::rngs::StdRng;
#[derive(Serialize)]
struct Data {
values: Vec<i32>,
sum: i32,
}
#[test]
fn test_data_processing() {
let mut rng = StdRng::seed_from_u64(123);
let config = GeneratorConfig::default();
let generator = VecGenerator::new(IntGenerator::new(0, 10), 1, 5);
let mut snapshots = PropertySnapshots::new("data_processing");
for _ in 0..3 {
let values = generator.generate(&mut rng, &config);
let sum: i32 = values.iter().sum();
let data = Data { values, sum };
snapshots.assert_json_snapshot(&data);
}
}§Debug Snapshot Testing
use protest::{Generator, primitives::IntGenerator, config::GeneratorConfig};
use protest_insta::PropertySnapshots;
use rand::SeedableRng;
use rand::rngs::StdRng;
#[test]
fn test_computation_output() {
let mut rng = StdRng::seed_from_u64(456);
let config = GeneratorConfig::default();
let generator = IntGenerator::new(1, 100);
let mut snapshots = PropertySnapshots::new("computation");
for _ in 0..5 {
let n = generator.generate(&mut rng, &config);
let result = (1..=n).sum::<i32>();
snapshots.assert_debug_snapshot(&result);
}
}Structs§
- Property
Snapshots - Helper for managing property-based snapshots
Functions§
- property_
snapshot_ test - Helper function to run a property-based test with snapshot assertions