xpct is an extensible test assertion library for Rust. It's designed to be ergonomic, batteries-included, and test framework agnostic.
Want to get started? Check out the tutorial.
xpct is extensible. In addition to allowing you to write custom matchers, it separates the logic of matchers from how they format their output, meaning you can:
This crate aims to provide many useful matchers out of the box. Check out the full list of provided matchers.
A simple equality assertion, like assert_eq
:
```rust,should_panic use xpct::{expect, equal};
expect!("disco").to(equal("Disco")); ```
Unwrapping a Some
value to make an assertion on the wrapped value:
```rust,shouldpanic use xpct::{begt, be_some, expect};
expect!(Some(41)) .to(besome()) .to(begt(57)); ```
Making assertions about individual fields of a struct:
```rust,shouldpanic use xpct::{beempty, bein, betrue, expect, fields, haveprefix, matchfields, not, why};
struct Player { id: String, name: String, level: u32, is_superstar: bool, }
let player = Player { id: String::from("REV12-62-05-JAM41"), name: String::from(""), level: 21, is_superstar: false, };
expect!(player).to(matchfields(fields!(Player { id: haveprefix("REV"), name: not(beempty()), level: bein(1..=20), issuperstar: why(betrue(), "only superstars allowed"), }))); ```
Making assertions about elements in a collection:
```rust,shouldpanic use xpct::{bein, containsubstr, equal, expect, havelen, match_elements};
let items = vec!["apple", "pear", "banana"];
expect!(items) .to(havelen(3)) .to(matchelements([ containsubstr("ana"), equal("pear"), bein(["mango", "orange"]), ])); ```
Showing rich diffs of data structures:
```rust,shouldpanic use xpct::{eqdiff, expect};
expect!(["apple", "banana"]).to(eq_diff(["banana", "orage"])); ```
The last two stable Rust releases are supported. Older releases may be supported as well.
The MSRV will only be increased when necessary—not every time there is a new Rust release. An increase in the MSRV will be accompanied by a minor semver bump if >=1.0.0 or a patch semver bump if <1.0.0.
Prior to version 1.0.0, breaking changes will be accompanied by a minor version bump, and new features and bug fixes will be accompanied by a patch version bump.