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

Doc Tests क्या हैं

Doc tests आपकी documentation के अंदर छोटे code examples हैं जिन्हें Rust असल में चलाता है, यह सुनिश्चित करने के लिए कि आपके docs कभी झूठ न बोलें।
Syntax
rust
/// Description.
///
/// ```
/// let result = crate_name::function(arg);
/// assert_eq!(result, expected);
/// ```
fn function() {}

Writing a Doc Test

किसी function के ऊपर /// documentation comment के अंदर एक fenced code block अपने आप एक runnable example माना जाता है और cargo test से execute होता है।

उदाहरण: Writing a Doc Test

markup
/// Adds one to the given number.
///
/// ```
/// let result = my_project::add_one(5);
/// assert_eq!(result, 6);
/// ```
pub fn add_one(n: i32) -> i32 {
    n + 1
}

fn main() {
    println!("add_one(5) = {}", add_one(5));
}

Doc Tests Keep Examples Honest

चूंकि doc comment में example असल में compile और execute होता है, अगर function का behavior कभी ऐसे बदले जो example को तोड़ दे, cargo test fail हो जाएगा और आपको alert करेगा।

उदाहरण: Doc Tests Keep Examples Honest

markup
/// Doubles the given number.
///
/// ```
/// assert_eq!(my_project::double(3), 6);
/// ```
pub fn double(n: i32) -> i32 {
    n * 2
}

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

Multiple Assertions in One Doc Test

एक single doc test के code block में कई statements और कई assertions हो सकते हैं, बिल्कुल एक ordinary test function की तरह, एक साथ कुछ related cases cover करने के लिए।

उदाहरण: Multiple Assertions in One Doc Test

markup
/// Checks whether a number is positive.
///
/// ```
/// assert!(my_project::is_positive(5));
/// assert!(!my_project::is_positive(-3));
/// ```
pub fn is_positive(n: i32) -> bool {
    n > 0
}

fn main() {
    println!("is_positive(5) = {}", is_positive(5));
    println!("is_positive(-3) = {}", is_positive(-3));
}

Running Doc Tests

cargo test अपने आप unit और integration tests के साथ हर doc test discover और चलाता है, तीनों तरह के tests verify करने के लिए एक unified command देते हुए।

उदाहरण: Running Doc Tests

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. doc comment में triple backticks में wrap किए बिना एक example लिखना, इसलिए cargo test इसे कभी एक doc test के रूप में पहचानता नहीं।
  2. function का behavior बदलने के बाद एक doc example को stale होने देना, जब cargo test ने mismatch अपने आप पकड़ लिया होता।
  3. यह भूल जाना कि doc tests को असल में compile और सफलतापूर्वक चलना चाहिए, इनके अंदर रखी किसी भी assert_eq! checks सहित।
चैप्टर सारांश
  • triple backticks से fenced, किसी /// doc comment के अंदर एक code block अपने आप compile और एक test के रूप में चलता है।
  • Doc tests documentation examples को accurate रखते हैं, क्योंकि एक टूटा example cargo test को बिल्कुल एक normal test की तरह fail कराता है।
  • assert_eq! और similar macros किसी doc test के code block के अंदर सीधे behavior verify करने के लिए उपयोग किए जा सकते हैं।
  • Doc tests cargo test के हिस्से के रूप में चलते हैं, unit और integration 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.