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

Unit Tests लिखना

Unit tests छोटे checks हैं जो आप यह सुनिश्चित करने के लिए लिखते हैं कि आपके code का एक specific टुकड़ा बिल्कुल वही करता रहे जो आप उम्मीद करते हैं।
Syntax
rust
#[cfg(test)]
mod tests {
    use super::*;

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

Marking a Function as a Test

#[test] attribute Cargo के test runner को बताता है कि एक function एक test case है, जिसे यह execute करेगा और जब आप cargo test चलाएं तो इसका result report करेगा।

उदाहरण: Marking a Function as a Test

markup
// Define the function `add` taking `a`, `b` and returning an integer
fn add(a: i32, b: i32) -> i32 {
    a + b
}

// Mark the following function as a unit test
#[test]
// Define the function `test_add` with no parameters
fn test_add() {
    // Assert that add(2, 3), 5 — panics if this is false
    assert_eq!(add(2, 3), 5);
}

// Entry point: execution of the program starts here
fn main() {
    // Print "add(2, 3) = {}" to the console, substituting in `add(2, 3)` (with a trailing newline)
    println!("add(2, 3) = {}", add(2, 3));
}

Organizing Tests in a Module

Convention unit tests को #[cfg(test)] से annotated एक nested module में रखता है, ताकि test code सिर्फ tests चलाते समय compile हो, regular release builds में included न हो।

उदाहरण: Organizing Tests in a Module

markup
// Define the function `multiply` taking `a`, `b` and returning an integer
fn multiply(a: i32, b: i32) -> i32 {
    a * b
}

// Attribute: #[cfg(test)]
#[cfg(test)]
// Declare a module named `tests`
mod tests {
    // Bring `super::*` into scope
    use super::*;

    // Mark the following function as a unit test
    #[test]
    // Define the function `test_multiply` with no parameters
    fn test_multiply() {
        // Assert that multiply(3, 4), 12 — panics if this is false
        assert_eq!(multiply(3, 4), 12);
    }
}

// Entry point: execution of the program starts here
fn main() {
    // Print "multiply(3, 4) = {}" to the console, substituting in `multiply(3, 4)` (with a trailing newline)
    println!("multiply(3, 4) = {}", multiply(3, 4));
}

Running Tests with cargo test

cargo test project को test mode में compile करता है, हर #[test] से चिह्नित function discover करता है, सभी को चलाता है, और एक pass/fail summary print करता है।

उदाहरण: Running Tests with cargo test

bash
cargo test

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

Testing Multiple Cases

एक function को अक्सर typical inputs, edge cases, और boundary conditions cover करने वाले कई test cases से exercise किया जाता है, हर एक अपने खुद के #[test] function के रूप में।

उदाहरण: Testing Multiple Cases

markup
// Define the function `is_even` taking `n` and returning a boolean
fn is_even(n: i32) -> bool {
    n % 2 == 0
}

// Mark the following function as a unit test
#[test]
// Define the function `test_even_number` with no parameters
fn test_even_number() {
    // Assert that is_even(4) — panics if this is false
    assert!(is_even(4));
}

// Mark the following function as a unit test
#[test]
// Define the function `test_odd_number` with no parameters
fn test_odd_number() {
    // Assert that !is_even(7) — panics if this is false
    assert!(!is_even(7));
}

// Entry point: execution of the program starts here
fn main() {
    // Print "is_even(4) = {}" to the console, substituting in `is_even(4)` (with a trailing newline)
    println!("is_even(4) = {}", is_even(4));
    // Print "is_even(7) = {}" to the console, substituting in `is_even(7)` (with a trailing newline)
    println!("is_even(7) = {}", is_even(7));
}
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. किसी test function के ऊपर #[test] attribute भूल जाना, मतलब cargo test इसे पहचानेगा या चलाएगा नहीं।
  2. #[cfg(test)] module के बाहर test functions लिखना, जिससे वे अनावश्यक रूप से regular release binary में compile हो जाते हैं।
  3. किसी function के public, stable behavior को test करने के बजाय बार-बार बदलने वाले implementation details test करना।
चैप्टर सारांश
  • #[test] किसी function को एक test के रूप में चिह्नित करता है जिसे cargo test discover और चलाएगा।
  • Tests convention से एक #[cfg(test)] mod tests { ... } block के अंदर रखे जाते हैं, उन्हें release builds से अलग रखते हुए।
  • assert_eq!, assert!, और assert_ne! tests के अंदर expected outcomes जांचने के लिए उपयोग होने वाले primary macros हैं।
  • cargo test हर discovered test compile और चलाता है, हर एक के लिए pass/fail results report करते हुए।
🔒

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.