2.6. プロジェクトテストの実行


コマンドラインで cargo プロジェクトでテストを実行するには、プロジェクトディレクトリーに移動し、以下のように cargo ツールを実行します。

  • Red Hat Enterprise Linux 7 の場合:

    $ scl enable rust-toolset-1.35 'cargo test'
    Copy to Clipboard Toggle word wrap
  • Red Hat Enterprise Linux 8 の場合:

    $ cargo test
    Copy to Clipboard Toggle word wrap

デフォルトでは、プロジェクトはデバッグモードでテストされています。リリースモードでプロジェクトをテストするには、以下のように --release オプションを指定して cargo ツールを実行します。

  • Red Hat Enterprise Linux 7 の場合:

    $ scl enable rust-toolset-1.35 'cargo test --release'
    Copy to Clipboard Toggle word wrap
  • Red Hat Enterprise Linux 8 の場合:

    $ cargo test --release
    Copy to Clipboard Toggle word wrap

例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);
}
Copy to Clipboard Toggle word wrap

テストとして 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 measured
    Copy to Clipboard Toggle word wrap
  • Red 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
    Copy to Clipboard Toggle word wrap

cargo はまずプロジェクトを再構築し、次にプロジェクトにあるテストを実行します。テスト my_test に成功しました。

トップに戻る
Red Hat logoGithubredditYoutubeTwitter

詳細情報

試用、購入および販売

コミュニティー

Red Hat ドキュメントについて

Red Hat をお使いのお客様が、信頼できるコンテンツが含まれている製品やサービスを活用することで、イノベーションを行い、目標を達成できるようにします。 最新の更新を見る.

多様性を受け入れるオープンソースの強化

Red Hat では、コード、ドキュメント、Web プロパティーにおける配慮に欠ける用語の置き換えに取り組んでいます。このような変更は、段階的に実施される予定です。詳細情報: Red Hat ブログ.

会社概要

Red Hat は、企業がコアとなるデータセンターからネットワークエッジに至るまで、各種プラットフォームや環境全体で作業を簡素化できるように、強化されたソリューションを提供しています。

Theme

© 2025 Red Hat