我怎样才能获得货物测试的保险?

问题描述 投票:0回答:3

当我想测试 C++ 覆盖率时,我可以使用

-fprofile-arcs -ftest-coverage
构建我的程序,运行所有测试,然后运行
gcov
以获得覆盖率。

然而,谈到 Rust,我完全迷失了方向。我想做的是运行以下测试(在我的 Mac 上),并覆盖路径中的所有 Rust 代码

components/raftstore

cargo test --package tests --test failpoints cases::test_normal
cargo test --package tests --test failpoints cases::test_bootstrap
cargo test --package tests --test failpoints cases::test_compact_log

this post,它说先运行

cargo test --no-run
,然后运行
kcov
。但是,当我真正这样做时,kcov 会永远阻塞。

然后我找到一个叫

cargo kcov
的东西,他提供
--test
。但是,当我像在
cargo kcov --test failpoints cases::test_normal
中那样运行
cargo test
时,我得到错误

error: cargo subcommand failure
note: cargo test exited with code exit status: 101
error: no test target named `failpoints`

我尝试了很多方法来解决这个问题,但是,它们都不起作用,所以我想知道我是否可以在这里得到一些帮助。

我知道还有其他覆盖工具,例如

tarpaulin
grcov
,我目前正在尝试这些工具。如果这些覆盖工具有简洁的解决方案,那也是可以接受的。 不过,我还是想知道
kcov
cargo-kcov
有什么问题。

unit-testing rust code-coverage rust-cargo kcov
3个回答
3
投票

根据 rustc 文档,现在可以获得基于检测的代码覆盖率。

以下命令生成覆盖率结果。请注意,它需要 Rust 分析器运行时,它默认包含在

nightly
.

RUSTFLAGS="-C instrument-coverage" \
    cargo test --tests

这些结果可能包括损坏的符号名称,可以使用 rustfilt 解决:

cargo install rustfilt

使用此设置运行测试后,应该有一个或多个

.profraw
文件。如果有多个,可以合并:

$ llvm-profdata merge -sparse default_*.profraw -o your_crate.profdata

然后可以使用 llvm-cov 显示覆盖信息,例如在本节中描述的(替换您的箱子和测试二进制文件的名称)。

如果 rustc 和 llvm-profdata resp 的 LLVM 版本可能会出现问题。 llvm-cov 不匹配,如here.

所述

这个答案基于 rustc 书的这一部分,它提供了更多关于代码覆盖率的信息。


2
投票

我喜欢将 tarpaulin 与 HTML 输出一起使用。

cargo tarpaulin --out Html

这应该创建一个名为

tarpaulin-report.html
的输出文件。在浏览器中打开此文件以查看代码覆盖率报告。 👍


0
投票

您可以通过以下方式为您的项目生成基于源的覆盖率:

  • Cargo.toml
    中启用覆盖率配置文件:

    ...
    [build]
    profiler = true
    ...
    
  • 安装分解器

    cargo install rustfilt
    
  • 添加乐器覆盖范围

    export RUSTFLAGS="-Cinstrument-coverage=all"
    
  • 根据您的操作系统安装依赖项

    openssl-devel
    libssl-devel
    llvm
    genhtml

  • 安装 Mozilla 的 grcov 并将分析器与 rust 编译器链接

    cargo install grcov
    rustup component add llvm-tools-preview
    
  • 运行测试

    cargo test --verbose
    
  • 生成代码覆盖信息

    grcov . -s . --binary-path ./target/debug/ -t lcov --branch --ignore-not-existing -o ./target/debug/
    
  • 根据覆盖率数据创建 HTML 报告

    genhtml -o ./target/debug/coverage/ --show-details --highlight --ignore-errors source --legend ./target/debug/lcov
    

您可以在

./target/debug/coverage/index.html

查看 HTML 报告
© www.soinside.com 2019 - 2024. All rights reserved.