Mutation Test: How does the cake work?

I’ve been a great fan of mutation test since I discovered a pit. When dived deeper, I wanted to check the status of the mutation test in the cake.
Starting with cargo-mutants
I found two casters for cake mutation test:
mutagen
Cargo-Mutants is still active.
I gave the sample code pass from my previous Java code:
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 -stage process:
- Install,
cargo install --locked cargo-mutants
- Use,
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 waited for a mutant to survive because I did not test the limit when the test value was equal to the limit. Strangely, cargo-mutants
did not detect.
Finding and correcting the problem
I searched the source code and found the place where the operators changed:
// 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, <
modified ==
And >
but not <=
. I bifurted the report and updated the code accordingly:
BinOp::Lt(_) => vec![quote! { == }, quote! {>}, quote!{ <= }],
BinOp::Gt(_) => vec![quote! { == }, quote! {<}, quote!{ => }],
I uploaded the new fork version:
cargo install --git https://github.com/nfrankel/cargo-mutants.git --locked
I revive the command:
cargo mutants
The output is this:
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 here missed.txt
file. I thought I fixed it and I was ready to make a pull cargo-mutants
Repo. I had to add the test to the limit:
#[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 all mutants were killed!
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
Solution
Many blog posts do not end with a desire to draw, but this:
Unfortunately, I could not provide the tests to pass; Fortunately, the warehouse caregiver helped me a lot. The desire to pull is combined: Enjoy this light improvement.
I learned more about cargo-mutants
and can improve the code in the process.
To go further:
Initially released on March 30, 2025 at A Java Geek