2025/06/07
目次
- 0 概要
- 1 基礎
- 2 可視化
- 3 カバレッジ計測の範囲無効
はじめに
この記事では、llvm-covのセットアップと、基本的なカバレッジ計測の方法を紹介する。
環境
以下のインストールをする。
$ cargo install cargo-llvm-cov
カバレッジ計測 1
まずは、そのままの状態でカバレッジ計測を行う。
環境によって、以下の llvm-tools-preview
のインストールを求められるので、流れに従ってインストールする。
$ cargo llvm-cov
info: cargo-llvm-cov currently setting cfg(coverage); you can opt-out it by passing --no-cfg-coverage
I will run `rustup component add llvm-tools-preview --toolchain stable-x86_64-unknown-linux-gnu` to install the `llvm-tools-preview` component for the selected toolchain.
Proceed? [Y/n]
info: downloading component 'llvm-tools'
info: installing component 'llvm-tools'
37.3 MiB / 37.3 MiB (100 %) 16.9 MiB/s in 2s
Compiling coverage v0.1.0 (/<path>/<to>/<project>/coverage)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.16s
Running unittests src/main.rs (target/llvm-cov-target/debug/deps/coverage-90ad100d3a119901)
running 0 tests
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/<path>/<to>/<project>/coverage/src/main.rs 1 1 0.00% 1 1 0.00% 5 5 0.00% 0 0 -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 1 1 0.00% 1 1 0.00% 5 5 0.00% 0 0 -
これ、そのままのコードだと、 main()
の呼び出しはTestされていないので、カバレッジは0%となる。
Test追加 1
単純に main()
を呼び出すTestを追加する。
このtestは、カバレッジ計測のためだけに存在することになってしまい、賛否あるだろうが、ここではサンプルのために書いた。
src/main.rs
fn main() {
println!("Hello, world!");
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_main() {
main();
}
}
カバレッジ計測 2
testを追加したので、再度カバレッジ計測を行う。
$ make coverage
cargo llvm-cov
info: cargo-llvm-cov currently setting cfg(coverage); you can opt-out it by passing --no-cfg-coverage
Compiling coverage v0.1.0 (/<path>/<to>/<project>/coverage)
Finished `test` profile [unoptimized + debuginfo] target(s) in 0.17s
Running unittests src/main.rs (target/llvm-cov-target/debug/deps/coverage-90ad100d3a119901)
running 1 test
test tests::test_main ... ok
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Filename Regions Missed Regions Cover Functions Missed Functions Executed Lines Missed Lines Cover Branches Missed Branches Cover
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
/<path>/<to>/<project>/coverage/src/main.rs 2 0 100.00% 2 0 100.00% 6 0 100.00% 0 0 -
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
TOTAL 2 0 100.00% 2 0 100.00% 6 0 100.00% 0 0 -
test::test_main
の意味があるかどうかはともかく、カバレッジは100%となった。
おわりに
コマンドライン上で見える基礎的な手法はこれで終わり。
このままでは、カバー率は確認できるものの、行などの詳細までは見えないので、次のセクションでは、カバレッジの可視化を紹介する。
目次
- 0 概要
- 1 基礎
- 2 可視化
- 3 カバレッジ計測の範囲無効