2.6. プロジェクトテストの実行
コマンドラインで cargo プロジェクトでテストを実行するには、プロジェクトディレクトリーに移動し、以下のように cargo ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.35 'cargo test'Red Hat Enterprise Linux 8 の場合:
$ cargo test
デフォルトでは、プロジェクトはデバッグモードでテストされています。リリースモードでプロジェクトをテストするには、以下のように --release オプションを指定して cargo ツールを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.35 'cargo test --release'Red Hat Enterprise Linux 8 の場合:
$ cargo test --release
例2.5 cargo を使用したプロジェクトのテスト
この例では、例2.2「cargo を使用したプロジェクトのビルド」 従って Rust プロジェクト helloworld を正常に構築していることを前提としています。
ディレクトリー helloworld に移動し、以下のソースコードが含まれるファイル src/main.rs を編集します。
fn main() {
println!("Hello, world!");
}
#[test]
fn my_test() {
assert_eq!(21+21, 42);
}
テストとして my_test マークされた関数が追加されました。
ファイルを保存し、テストを実行します。
Red Hat Enterprise Linux 7 の場合:
$ scl enable rust-toolset-1.35 'cargo test' Compiling helloworld v0.1.0 (file:///home/vslavik/Documentation/rusttest/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.26 secs Running target/debug/deps/helloworld-9dd6b83647b49aec running 1 test test my_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measuredRed Hat Enterprise Linux 8 の場合:
$ cargo test Compiling helloworld v0.1.0 (file:///home/vslavik/Documentation/rusttest/helloworld) Finished dev [unoptimized + debuginfo] target(s) in 0.26 secs Running target/debug/deps/helloworld-9dd6b83647b49aec running 1 test test my_test ... ok test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured
cargo はまずプロジェクトを再構築し、次にプロジェクトにあるテストを実行します。テスト my_test に成功しました。