Integration Tests क्या हैं
In this page:
// 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
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
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));
}
Login to try C/C++/Java/PHP code in the editor
Why Integration Tests Matter
Unit tests individual pieces को isolation में verify करते हैं, जबकि integration tests confirm करते हैं कि वे pieces असल में सही से साथ काम करते हैं जब एक असली caller उन्हें उपयोग करने के तरीके से उपयोग किया जाए।
उदाहरण: Why Integration Tests Matter
// 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"));
}
Login to try C/C++/Java/PHP code in the editor
Running All Tests Together
cargo test src/ से unit tests को tests/ से हर integration test file के साथ compile और चलाता है, results का एक combined summary report करते हुए।
उदाहरण: Running All Tests Together
cargo test
⚠️ Run this in your own terminal or Node.js environment.
- top-level
tests/directory के बजाय integration tests कोsrc/के अंदर रखना, जिसे Cargo अलग तरीके से treat करता है। - यह भूल जाना कि integration tests सिर्फ किसी crate की public API call कर सकते हैं, इसके private internal functions नहीं।
- यह मान लेना कि एक single
tests/file सब कुछ cover करती है -- Cargotests/में हर 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 testdefault रूप से unit tests (src/में) और integration tests (tests/में) दोनों एक साथ चलाता है।
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: