انواع دیگر تست‌ها

Integration Tests

اگر می‌خواهید کتابخانه خود را به عنوان یک سرویس‌گیرنده آزمایش کنید، از تست یکپارچه‌سازی (integration test) استفاده کنید.

یک فایل .rs در زیر tests/ بسازید:

// tests/my_library.rs use my_library::init; #[test] fn test_init() { assert!(init().is_ok()); }

این آزمایش‌ها فقط به public API مربوط به crate شما دسترسی دارند.

تست‌ سندها

زبان Rust دارای پشتیبانی داخلی برای تست‌های مستندسازی است:

#![allow(unused)] fn main() { /// Shortens a string to the given length. /// /// ``` /// # use playground::shorten_string; /// assert_eq!(shorten_string("Hello World", 5), "Hello"); /// assert_eq!(shorten_string("Hello World", 20), "Hello World"); /// ``` pub fn shorten_string(s: &str, length: usize) -> &str { &s[..std::cmp::min(length, s.len())] } }
  • بلوک‌های کد در commentها /// به طور خودکار به عنوان کد Rust دیده می‌شوند.
  • این کد به عنوان بخشی از cargo test کامپایل و اجرا می‌شود.
  • افزودن # به کد، آن را از مستندات پنهان می‌کند، اما همچنان آن را کامپایل/اجرا می‌کند.
  • کد بالا را در Rust Playground تست کنید.