https://github.com/google/assertor
Fluent assertion library for Rust with readable messages.
https://github.com/google/assertor
assert assertions rust-library testing
Last synced: 4 days ago
JSON representation
Fluent assertion library for Rust with readable messages.
- Host: GitHub
- URL: https://github.com/google/assertor
- Owner: google
- License: apache-2.0
- Created: 2021-09-06T16:27:28.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2026-03-27T18:03:38.000Z (9 days ago)
- Last Synced: 2026-03-29T13:38:12.896Z (8 days ago)
- Topics: assert, assertions, rust-library, testing
- Language: Rust
- Homepage:
- Size: 184 KB
- Stars: 149
- Watchers: 4
- Forks: 15
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
Assertor
========
Assertor makes test assertions and failure messages more human-readable.
[](https://crates.io/crates/assertor)
[](https://github.com/google/assertor/LICENSE)
[](https://docs.rs/crate/assertor/)
[](https://api.securityscorecards.dev/projects/github.com/google/assertor)
Assertor is heavily affected by [Java Truth](https://github.com/google/truth) in terms of API design and error messages,
but this is a totally different project from Java Truth.
### Disclaimer
This is not an official Google product, it is just code that happens to be owned by Google.
⚠ The API is not fully stable and may be changed until version 1.0.
Example
-------
```rust
use assertor::*;
#[test]
fn test_it() {
assert_that!("foobarbaz").contains("bar");
assert_that!("foobarbaz").ends_with("baz");
assert_that!(0.5).with_abs_tol(0.2).is_approx_equal_to(0.6);
assert_that!(vec!["a", "b"]).contains("a");
assert_that!(vec!["a", "b"]).has_length(2);
assert_that!(vec!["a", "b"]).contains_exactly(vec!["a", "b"]);
assert_that!(Option::Some("Foo")).has_value("Foo");
}
```
### Failure cases
```rust
use assertor::*;
fn test_it() {
assert_that!(vec!["a", "b", "c"]).contains_exactly(vec!["b", "c", "d"]);
// missing (1) : ["d"]
// unexpected (1): ["a"]
// ---
// expected : ["b", "c", "d"]
// actual : ["a", "b", "c"]
}
```
## anyhow
Supports asserting error value of `anyhow` under `anyhow` feature flag.
```toml
[dependencies]
assertor = { version = "*", features = ["anyhow"] }
```
```rust
use assertor::*;
use anyhow::anyhow;
fn anyhow_func() -> anyhow::Result<()> {
Err(anyhow!("failed to parse something in foobar"))
}
fn test_it() {
assert_that!(anyhow_func()).err().has_message("failed to parse something in foobar");
assert_that!(anyhow_func()).err().as_string().contains("parse something");
assert_that!(anyhow_func()).err().as_string().starts_with("failed");
assert_that!(anyhow_func()).err().as_string().ends_with("foobar");
}
```
## Feature ideas
- [ ] Color / Bold
- [ ] Better diff: vec
- [ ] Better diff: set
- [ ] Better diff: HashMap