5

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.

Francis Gagné
  • 60,274
  • 7
  • 180
  • 155
redtankd
  • 53
  • 4

1 Answers1

8

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.)

Jan Zerebecki
  • 825
  • 7
  • 18