Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

rust

kcov

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.

like image 674
redtankd Avatar asked Feb 24 '17 13:02

redtankd


1 Answers

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

like image 102
Jan Zerebecki Avatar answered Nov 15 '22 08:11

Jan Zerebecki