prop-check-rs
prop-check-rs is a property-based testing library written in Rust. It leverages functional programming concepts to efficiently generate and validate test data.
Read this in other languages: 日本語
What is Property-Based Testing?
Property-based testing is a testing methodology where instead of testing specific input values, you define properties that your program should satisfy and then verify these properties against a large number of randomly generated inputs. This approach helps discover edge cases that developers might not have anticipated.
Features
- Rich generators: Easily generate test data of various types
- Functional programming style: Composable API utilizing monads
- State-based testing: Support for state machine simulation
- Advanced customization: Define your own generators and properties
Installation
Add the following to your Cargo.toml:
[]
= "0.0.862"
Basic Usage
1. Simple Property Test
Here's an example testing a property about list length:
use Gens;
use ;
use RNG;
use Result;
2. Choosing Values
use Gens;
use ;
use RNG;
use Result;
3. Using Sized Generators
use Gens;
use ;
use RNG;
use Result;
Key Components
Gen
Gen<A> is a generator for values of type A. It provides methods like map, flat_map, and and_then to create new generators from existing ones.
// Generator for integers from 1 to 100
let int_gen = choose_i32;
// Generator that converts integers to strings
let string_gen = int_gen.map;
Gens
Gens is a factory for creating various generators. It provides generators for:
- Basic types (integers, floating-point numbers, characters, booleans, etc.)
- Lists
- Optional values
- Choosing one from multiple options
- Probability-based selection
// Generators for basic types
let int_gen = one_i32;
let float_gen = one_f64;
let bool_gen = one_bool;
// Generator with a specified range
let range_gen = choose_i32;
// Generator for lists
let list_gen = list_of_n;
// Generator that chooses one from multiple options
let choice_gen = one_of_values;
// Generator based on probabilities
let weighted_gen = frequency_values;
Prop
Prop is a structure representing a property. A property defines a condition to verify against values generated by a generator.
// Property verifying that integers are always positive
let positive_prop = for_all_gen;
// Combining multiple properties
let combined_prop = positive_prop.and;
State<S, A>
State<S, A> is a monad representing a computation with state S that produces a value of type A. This allows composing computations while maintaining state.
// Get the state
let get_state = get;
// Set the state
let set_state = set;
// Modify the state
let modify_state = modify;
// Compose stateful computations
let computation = get_state.flat_map;
Advanced Usage Examples
Testing State Machines
The machine.rs module provides an example of simulating a state machine. Here's an example of a candy vending machine simulation:
// Input
// State machine
// Simulating the state machine
let inputs = vec!;
let simulation = simulate_machine;
let result = simulation.run;
Creating Custom Generators
You can create your own generators to generate domain-specific test data:
// Generator for valid email addresses
// Usage example
let prop = for_all_gen;
Benchmarks
prop-check-rs includes optimizations for efficiently generating large amounts of test data. To run the benchmarks:
License
Licensed under either of
- Apache License, Version 2.0, (LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.