Unit Tests लिखना
In this page:
#[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
// 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));
}
Login to try C/C++/Java/PHP code in the editor
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
// 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));
}
Login to try C/C++/Java/PHP code in the editor
Running Tests with cargo test
cargo test project को test mode में compile करता है, हर #[test] से चिह्नित function discover करता है, सभी को चलाता है, और एक pass/fail summary print करता है।
उदाहरण: Running Tests with cargo test
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
// 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));
}
Login to try C/C++/Java/PHP code in the editor
- किसी test function के ऊपर
#[test]attribute भूल जाना, मतलबcargo testइसे पहचानेगा या चलाएगा नहीं। #[cfg(test)]module के बाहर test functions लिखना, जिससे वे अनावश्यक रूप से regular release binary में compile हो जाते हैं।- किसी function के public, stable behavior को test करने के बजाय बार-बार बदलने वाले implementation details test करना।
#[test]किसी function को एक test के रूप में चिह्नित करता है जिसेcargo testdiscover और चलाएगा।- 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: