How do I exclude test functions from code coverage when using kcov?

1.8k views Asked by At

By default, kcov includes all sources files, including test functions, in its code coverage results. This skews the reported coverage rate. How do I tell kcov to exclude test functions?

For example:

#[test]
fn foo() {
    ...
}

kcov reports coverage data for foo, but I want to exclude it.

1

There are 1 answers

0
Jan Zerebecki On BEST ANSWER

Have your testing code in different files and use the kcov arguments --exclude-path=./test (for the integration tests) or --exclude-pattern=_test.rs (if you move all your tests for foo.rs into foo_test.rs).

Another way is to use the kcov argument --exclude-region='#[cfg(test)]:#[cfg(testkcovstopmarker)]' and make sure that each file either only has tests at the end and no non-test code following it or adds the stop marker #[cfg(testkcovstopmarker)] with the necessary unused definition after it to make rustc happy. (kcov finds the start and end string for a region anywhere in a line.)

(There is also the possibility to exclude lines based on a string they contain, but that won't work for excluding tests in Rust.)