title | hide_table_of_contents | description | sidebar_position |
---|---|---|---|
Mutation Testing |
true |
Mutation testing finds code not tested. |
11 |
Mutation testing is making changes to a program, either manually or automatically, to identify changes that can be made that don't get caught by tests.
Mutation testing is similar to measuring code coverage. It's goal is to identify code not covered by tests, or possibly that looks like it's covered by tests because the lines of code are executing during a test, but the outcomes themselves such as contract function return value, or events published, are not tested.
The cargo-mutants
tool can be used to automatically and iteratively modify the Rust code, and rerun the tests after each mutation, to identify code not tested.
-
Install
cargo-mutants
:cargo install --locked cargo-mutants
-
Run the
cargo mutants
command inside your contract's crate directory.$ cargo mutants Found 4 mutants to test ok Unmutated baseline in 19.0s build + 0.6s test INFO Auto-set test timeout to 20s MISSED src/lib.rs:14:9: replace IncrementContract::increment -> u32 with 1 in 0.4s build + 0.4s test 4 mutants tested in 23s: 1 missed, 3 caught
Code that is identified as not covered by a test will be outputted as a
MISSED
line in the output.Diffs of each change that was attempted can be found in the
mutants.out/diff
directory.