← Back to Rust Course | Chapter 15: Cargo, Testing & Best Practices | Lesson 5 of 8

Integration Tests क्या हैं

Integration tests जांचते हैं कि आपके program के सभी अलग-अलग हिस्से असल में बाहर से एक साथ सही से काम करते हैं।
Syntax
rust
// tests/name.rs
use crate_name::function;

#[test]
fn name() {
    assert_eq!(function(input), expected);
}

The tests Directory

Cargo एक top-level tests/ directory को integration tests का घर मानता है, इसके अंदर हर .rs file को एक अलग test crate मानते हुए।

उदाहरण: The tests Directory

bash
my_project/
  src/
    lib.rs
  tests/
    integration_test.rs

⚠️ Run this in your own terminal or Node.js environment.

Integration Tests Use the Public API Only

चूंकि tests/ में हर file अपने खुद के external crate के रूप में compile होती है, यह सिर्फ उन functions को call कर सकती है जो library pub से publicly expose करती है, बिल्कुल library के किसी भी दूसरे user की तरह।

उदाहरण: Integration Tests Use the Public API Only

markup
pub fn add_two(n: i32) -> i32 {
    n + 2
}

// In tests/integration_test.rs this would look like:
// use my_project::add_two;
// #[test]
// fn test_add_two() {
//     assert_eq!(add_two(3), 5);
// }

fn main() {
    println!("add_two(3) = {}", add_two(3));
}

Why Integration Tests Matter

Unit tests individual pieces को isolation में verify करते हैं, जबकि integration tests confirm करते हैं कि वे pieces असल में सही से साथ काम करते हैं जब एक असली caller उन्हें उपयोग करने के तरीके से उपयोग किया जाए।

उदाहरण: Why Integration Tests Matter

markup
// Define the function `parse_and_double` taking `input` and returning a `Option<i32>`
fn parse_and_double(input: &str) -> Option<i32> {
    input.parse::<i32>().ok().map(|n| n * 2)
}

// Entry point: execution of the program starts here
fn main() {
    // Print "{:?}" to the console, substituting in `parse_and_double("21")` (with a trailing newline)
    println!("{:?}", parse_and_double("21"));
    // Print "{:?}" to the console, substituting in `parse_and_double("oops")` (with a trailing newline)
    println!("{:?}", parse_and_double("oops"));
}

Running All Tests Together

cargo test src/ से unit tests को tests/ से हर integration test file के साथ compile और चलाता है, results का एक combined summary report करते हुए।

उदाहरण: Running All Tests Together

bash
cargo test

⚠️ Run this in your own terminal or Node.js environment.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. top-level tests/ directory के बजाय integration tests को src/ के अंदर रखना, जिसे Cargo अलग तरीके से treat करता है।
  2. यह भूल जाना कि integration tests सिर्फ किसी crate की public API call कर सकते हैं, इसके private internal functions नहीं।
  3. यह मान लेना कि एक single tests/ file सब कुछ cover करती है -- Cargo tests/ में हर file को इसके अपने अलग test binary के रूप में compile करता है।
चैप्टर सारांश
  • Integration tests एक top-level tests/ directory में रहते हैं, src/ से अलग।
  • tests/ में हर file एक independent crate के रूप में compile होती है जो सिर्फ library की public API access कर सकती है।
  • Integration tests verify करते हैं कि किसी library के कई हिस्से एक external caller के perspective से सही से साथ काम करते हैं।
  • cargo test default रूप से unit tests (src/ में) और integration tests (tests/ में) दोनों एक साथ चलाता है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.