Test test: How works in rust?

I have been a great admire to test the boom since I discovered the hole. While deeper dive in rust, I wanted to check the rust tested condition.
Starting cargo-mutants
I found boxes to test the mutation in rust:
mutagen
It has not been preserved for three years, while goods officers are still under active development.
I have transferred the sample icon from the previous Java icon to rust:
struct LowPassPredicate {
threshold: i32,
}
impl LowPassPredicate {
pub fn new(threshold: i32) -> Self {
LowPassPredicate { threshold }
}
pub fn test(&self, value: i32) -> bool {
value < self.threshold
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn should_return_true_when_under_limit() {
let low_pass_predicate = LowPassPredicate::new(5);
assert_eq!(low_pass_predicate.test(4), true);
}
#[test]
fn should_return_false_when_above_limit() {
let low_pass_predicate = LowPassPredicate::new(5);
assert_eq!(low_pass_predicate.test(6), false);
}
}
Use cargo-mutants
It is a two -step process:
- Install it,
cargo install --locked cargo-mutants
- Use it,
cargo mutants
Found 4 mutants to test
ok Unmutated baseline in 0.1s build + 0.3s test
INFO Auto-set test timeout to 20s
4 mutants tested in 1s: 4 caught
I expected it to remain mutant, because I did not test the borders when the value of the test is equal to the limit. Strange enough, cargo-mutants
He did not discover that.
Find the problem and fix it
I searched in the source code and found the place where the operators mutated:
// We try replacing logical ops with == and !=, which are effectively
// XNOR and XOR when applied to booleans. However, they're often unviable
// because they require parenthesis for disambiguation in many expressions.
BinOp::Eq(_) => vec![quote! { != }],
BinOp::Ne(_) => vec![quote! { == }],
BinOp::And(_) => vec![quote! { || }],
BinOp::Or(_) => vec![quote! { && }],
BinOp::Lt(_) => vec![quote! { == }, quote! {>}],
BinOp::Gt(_) => vec![quote! { == }, quote! {<}],
BinOp::Le(_) => vec![quote! {>}],
BinOp::Ge(_) => vec![quote! {<}],
BinOp::Add(_) => vec![quote! {-}, quote! {*}],
actually, <
It changes to ==
and >
But not to <=
. I am eager on the ribo and I updated the symbol accordingly:
BinOp::Lt(_) => vec![quote! { == }, quote! {>}, quote!{ <= }],
BinOp::Gt(_) => vec![quote! { == }, quote! {<}, quote!{ => }],
You have installed the new branching version:
cargo install --git https://github.com/nfrankel/cargo-mutants.git --locked
I am re -executed:
cargo mutants
The output is the following:
Found 5 mutants to test
ok Unmutated baseline in 0.1s build + 0.3s test
INFO Auto-set test timeout to 20s
MISSED src/lib.rs:11:15: replace < with <= in LowPassPredicate::test in 0.2s build + 0.2s test
5 mutants tested in 2s: 1 missed, 4 caught
You can find the same information in missed.txt
file. I thought I fixed it and I was ready to apply for a withdrawal to cargo-mutants
Ribo. You just need to add the test to the borders:
#[test]
fn should_return_false_when_equals_limit() {
let low_pass_predicate = LowPassPredicate::new(5);
assert_eq!(low_pass_predicate.test(5), false);
}
cargo test
running 3 tests
test tests::should_return_false_when_above_limit ... ok
test tests::should_return_false_when_equals_limit ... ok
test tests::should_return_true_when_under_limit ... ok
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
cargo mutants
And kill all mutants!
Found 5 mutants to test
ok Unmutated baseline in 0.1s build + 0.2s test
INFO Auto-set test timeout to 20s
5 mutants tested in 2s: 5 caught
conclusion
Many blog publications do not end with a request for withdrawal, but this is not the following:
Unfortunately, I could not manage the tests. Fortunately, the warehouse supervisor – a lot. The withdrawal request is combined: Enjoy this slight improvement.
I learned more about cargo-mutants
The symbol in this process can be improved.
To go further:
It was originally published in Java, obsessed on March 30, 2025