統合テスト
testsディレクトリ
「統合テスト」はCoddyKit上の無料Learn Rust Codingレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはLearn Rust Coding学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Learn Rust Codingコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
What Are Integration Tests?
Integration tests verify your crate's public API the way an external user would. They live outside src and can only call public items.
The tests Directory
Cargo treats every file in the top-level tests/ directory as a separate test crate. No #[cfg(test)] module is needed there.
tests/api.rstests/parsing.rs
A Library to Test
Integration tests target a library crate. Suppose src/lib.rs exposes a public function.
// src/lib.rs
pub fn add(a: i32, b: i32) -> i32 {
a + b
}Writing an Integration Test
In tests/add.rs, import the crate by name and test its public API. Each function still uses #[test].
// tests/add.rs
use my_lib::add;
#[test]
fn adds_two_numbers() {
assert_eq!(add(2, 2), 4);
}Public Only
Because integration tests are a separate crate, they can use only public (pub) items. Private functions are invisible — just like to any real consumer.
Running Integration Tests
cargo test runs unit and integration tests together. Each file under tests/ appears as its own test binary in the output.
cargo testRunning One File
Use --test with the file name (without extension) to run a single integration test file.
cargo test --test addShared Test Helpers
To share setup code without making it a test file, put helpers in tests/common/mod.rs. Files in subdirectories are not treated as test crates.
// tests/common/mod.rs
pub fn setup() {
// shared setup logic
}Using the Helper
Declare the module and call its functions from your integration test.
// tests/api.rs
mod common;
#[test]
fn uses_setup() {
common::setup();
assert!(true);
}Unit vs Integration
Two complementary test styles:
- Unit tests live in
src, can test private items, fast and focused - Integration tests live in
tests/, test the public API end to end
Testing a Binary Crate
Integration tests target libraries, not binaries. A common pattern is to keep logic in src/lib.rs and have a thin src/main.rs that calls it, so both unit and integration tests can reach the code.
// src/main.rs
fn main() {
println!("{}", my_lib::add(2, 3));
}Quick Check
Where do integration tests live, and what can they access?
Recap
You learned integration testing:
- Files in
tests/are separate test crates - They import your crate by name and test only public items
cargo test --test nameruns one file- Shared helpers go in
tests/common/mod.rs
よくある質問
「統合テスト」レッスンは無料ですか?
はい。「統合テスト」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Learn Rust Codingコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Learn Rust Codingコースには全4レッスンが含まれています。
「統合テスト」で何を学びますか?
testsディレクトリ ブラウザで直接実行するハンズオンコードでLearn Rust Codingを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Learn Rust Codingを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLearn Rust Codingは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。
「統合テスト」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLearn Rust Codingレッスンでコードを書いて実行できますか?
はい。すべてのLearn Rust Codingレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。