Important
We’re excited you’re checking out Hegel! Hegel is in beta, and we’d love for you to try it and report any feedback.
As part of our beta, we may make breaking changes if it makes Hegel a better property-based testing library. If that instability bothers you, please check back in a few months for a stable release!
See https://hegel.dev/compatibility for more details.
Hegel for Rust
hegel-rust is a property-based testing library for Rust. hegel-rust is based on Hypothesis, using the Hegel protocol.
Installation
To install: cargo add --dev hegeltest.
Quickstart
Here’s a quick example of how to write a Hegel test:
use hegel::generators as gs;
use hegel::TestCase;
fn my_sort(ls: &[i32]) -> Vec<i32> {
let mut result: Vec<i32> = ls.to_vec();
result.sort();
result.dedup();
result
}
#[hegel::test]
fn test_matches_builtin(tc: TestCase) {
let mut vec1 = tc.draw(gs::vecs(gs::integers::<i32>()));
let vec2 = my_sort(&vec1);
vec1.sort();
assert_eq!(vec1, vec2);
}
This test will fail when run with cargo test! Hegel will produce a minimal failing test case for us:
---- test_matches_builtin stdout ----
let vec1 = vec![0, 0];
thread 'test_matches_builtin' (2) panicked at tests/readme.rs:16:5:
assertion \`left == right\` failed
left: [0, 0]
right: [0]
Hegel reports the minimal example showing that our sort is incorrectly dropping duplicates. If we remove result.dedup() from my_sort(), this test will then pass (because it’s just comparing the standard sort against itself).
Summary
Property-based testing for Rust built on Hypothesis (hegel.dev family, 371 stars, MIT): #[hegel::test] with composable generators, universal protocol shared with Go/TS/OCaml bindings via hegel-core.
Learnings
- The cross-language angle is the differentiator vs Rust-native proptest: same generators/shrinking across languages. See Property-based testing.
Gotchas
- (none yet)
Hands-on
(research only)