为什么kcov为Rust程序计算不正确的代码覆盖率统计信息? [英] Why does kcov calculate incorrect code coverage statistics for Rust programs?

查看:226
本文介绍了为什么kcov为Rust程序计算不正确的代码覆盖率统计信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用



lib.rs <的覆盖范围是正确的:





但覆盖范围对于 subm.rs 是错误的!请注意,该函数是公共函数,因此无法从库中对其进行优化:





在这里我们可以验证 kcov 是否可以正常工作,因为它可以计算一个文件的代码覆盖率,但是不能看到第二个文件



这里出了什么问题?也许测试二进制文件剥离了未使用的功能,而kcov看不到它们?

解决方案

您是正确的:完全未使用的功能在处被剥离。此刻,因此像kcov这样的覆盖率工具仅适合于已使用功能(至少是此类工具的摘要功能)内的分支覆盖率。关于一些讨论对于测试/调试版本,默认情况下不会发生这种情况。


I've tried to use kcov to get code coverage for a Rust library. I've followed this tutorial to build and use kcov. The coverage seems to work, however I'm facing a strange high coverage. Some files in the project gets a 100% coverage, even if they are actually not covered at all!

This is a minimal project reproducing the problem:

Cargo.toml

[package]
name = "mypackage"
version = "0.1.0"
authors = ["mbrt"]

src/lib.rs

pub mod subm;

pub fn coverage1(i : bool) -> bool {
    if i {
        true
    }
    else {
        false
    }
}

#[cfg(test)]
mod test {
    use super::coverage1;

    #[test]
    fn test_coverage1() {
        assert!(coverage1(true));
    }
}

src/subm.rs

pub fn coverage2(i : bool) -> bool {
    if i {
        true
    }
    else {
        false
    }
}

#[cfg(test)]
mod test {
    #[test]
    fn test_coverage2() {
    }
}

There are two identical functions, one in the root of the crate, and another in a submodule. The only difference is that the first test stimulates one function, and the other does nothing at all. In this case I'd expect a coverage not greater than 50%.

However kcov reports this:

The coverage for lib.rs is correct:

But the coverage for subm.rs is wrong! Note that the function is public, so it cannot be optimized out from the library:

Here we can verify that kcov is working, because it is able to compute code coverage for one file, but it is not able to see that the second file is not covered at all.

What is the problem here? Maybe test binaries strip down unused functions and kcov cannot see them?

解决方案

You're correct: totally unused functions are stripped at the moment, so coverage tools like kcov are only good for branch coverage within used functions (at least, the summary functionality of such tools). There is some discussion about making this not happen by default for test/debug builds.

这篇关于为什么kcov为Rust程序计算不正确的代码覆盖率统计信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆