开发编程
#testing
rust-testing
Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology.
DeepseekModel
官方收录技能
质量 优秀 · 90
v1.0.0
获取
https://deepseekmodel.com/api/download.php?id=affaan-m-ecc-docs-tr-skills-rust-testing-skill-md&format=skill
下载 .skill
标准格式,含 system_prompt 与 model_config,导入任意 Agent 框架即可使用
.skill 文件中 system_prompt 字段的实际内容。
name rust-testing description Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology. origin ECC Rust Test Desenleri TDD metodolojisini takip ederek güvenilir, bakım yapılabilir testler yazmak için kapsamlı Rust test desenleri. Ne Zaman Kullanılır Yeni Rust fonksiyonları, metotları veya trait'leri yazma Mevcut koda test kapsamı ekleme Performans-kritik kod için benchmark'lar oluşturma Girdi doğrulama için property-based testler uygulama Rust projelerinde TDD iş akışını takip etme Nasıl Çalışır Hedef kodu tanımla — Test edilecek fonksiyon, trait veya modülü bul Bir test yaz — #[cfg(test)] modülünde #[test] kullan, parametreli testler için rstest veya property-based testler için proptest Bağımlılıkları mock'la — Test altındaki birimi izole etmek için mockall kullan Testleri çalıştır (RED) — Testin beklenen hata ile başarısız olduğunu doğrula Uygula (GREEN) — Geçmek için minimal kod yaz Refactor — Testleri yeşil tutarken iyileştir Kapsamı kontrol et — cargo-llvm-cov kullan, 80%+ hedefle Rust için TDD İş Akışı RED-GREEN-REFACTOR Döngüsü RED → Önce başarısız bir test yaz GREEN → Testi geçmek için minimal kod yaz REFACTOR → Testleri yeşil tutarken kodu iyileştir REPEAT → Bir sonraki gereksinimle devam et Rust'ta Adım-Adım TDD // RED: Önce testi yaz, yer tutucu olarak todo!() kullan pub fn add (a: i32 , b: i32 ) -> i32 { todo!() } #[cfg(test)] mod tests { use super::*; #[test] fn test_add () { assert_eq! ( add ( 2 , 3 ), 5 ); } } // cargo test → 'not yet implemented'da panic // GREEN: todo!()'yu minimal implementasyonla değiştir pub fn add (a: i32 , b: i32 ) -> i32 { a + b } // cargo test → GEÇTİ, sonra testleri yeşil tutarken REFACTOR Unit Testler Modül Seviyesi Test Organizasyonu // src/user.rs pub struct User { pub name: String , pub email: String , } impl User { pub fn new (name: impl Into < String >, email: impl Into < String >) -> Result < Self , String > { let email = email. into (); if !email. contains ( '@' ) { return Err ( format! ( "invalid email: {email}" )); } Ok ( Self { name: name. into (), email }) } pub fn display_name (& self ) -> & str { & self .name } } #[cfg(test)] mod tests { use super::*; #[test] fn creates_user_with_valid_email () { let user = User:: new ( "Alice" , "alice@example.com" ). unwrap (); assert_eq! (user. display_name (), "Alice" ); assert_eq! (user.email, "alice@example.com" ); } #[test] fn rejects_invalid_email () { let result = User:: new ( "Bob" , "not-an-email" ); assert! (result. is_err ()); assert! (result. unwrap_err (). contains ( "invalid email" )); } } Assertion Makroları assert_eq! ( 2 + 2 , 4 ); // Eşitlik assert_ne! ( 2 + 2 , 5 ); // Eşitsizlik assert! ( vec! [ 1 , 2 , 3 ]. contains (& 2 )); // Boolean assert_eq! (value, 42 , "expected 42 but got {value}" ); // Özel mesaj assert! (( 0.1_f64 + 0.2 - 0.3 ). abs () < f64 ::EPSILON); // Float karşılaştırma Hata ve Panic Testi Result Dönüşlerini Test Etme #[test] fn parse_returns_error_for_invalid_input () { let result = parse_config ( "}{invalid" ); assert! (result. is_err ()); // Spesifik hata varyantını doğrula let err = result. unwrap_err (); assert! (matches!(err, ConfigError:: ParseError (_))); } #[test] fn parse_succeeds_for_valid_input () -> Result <(), Box < dyn std::error::Error>> { let config = parse_config ( r#"{"port": 8080}"# )?; assert_eq! (config.port, 8080 ); Ok (()) // Herhangi bir ? Err döndürürse test başarısız olur } Panic'leri Test Etme #[test] #[should_panic] fn panics_on_empty_input () { process (&[]); } #[test] #[should_panic(expected = "index out of bounds" )] fn panics_with_specific_message () { let v : Vec < i32 > = vec! []; let _ = v[ 0 ]; } Entegrasyon Testleri Dosya Yapısı my_crate/ ├── src/ │ └── lib.rs ├── tests/ # Entegrasyon testleri │ ├── api_test.rs # Her dosya ayrı bir test binary'si │ ├── db_test.rs │ └── common/ # Paylaşılan test yardımcıları │ └── mod.rs Entegrasyon Testleri Yazma // tests/api_test.rs use my_crate::{App, Config}; #[test] fn full_request_lifecycle () { let config = Config:: test_default (); let app = App:: new (config); let response = app. handle_request ( "/health" ); assert_eq! (response.status, 200 ); assert_eq! (response.body, "OK" ); } Async Testler Tokio ile #[tokio::test] async fn fetches_data_successfully () { let client = TestClient:: new (). await ; let result = client. get ( "/data" ). await ; assert! (result. is_ok ()); assert_eq! (result. unwrap ().items. len (), 3 ); } #[tokio::test] async fn handles_timeout () { use std::time::Duration; let result = tokio::time:: timeout ( Duration:: from_millis ( 100 ), slow_operation (), ). await ; assert! (result. is_err (), "should have timed out" ); } Test Organizasyon Desenleri rstest ile Parametreli Testler use rstest::{rstest, fixture}; #[rstest] #[case( "hello" , 5)] #[case( "" , 0)] #[case( "rust" , 4)] fn test_string_length ( #[case] input: & str , #[case] expected: usize ) { assert_eq! (input. len (), expected); } // Fixture'lar #[fixture] fn test_db () -> TestDb { TestDb:: new_in_memory () } #[rstest] fn test_insert (test_db: TestDb) { test_db. insert ( "key" , "value" ); assert_eq! (test_db. get ( "key" ), Some ( "value" . into ())); } Test Yardımcıları #[cfg(test)] mod tests { use super::*; /// Mantıklı varsayılanlarla test kullanıcısı oluşturur. fn make_user (name: & str ) -> User { User:: new (name, & format! ( "{name}@test.com" )). unwrap () } #[test] fn user_display () { let user = make_user ( "alice" ); assert_eq! (user. display_name (), "alice" ); } } proptest ile Property-Based Testing Temel Property Testleri use proptest::prelude::*; proptest! { #[test] fn encode_decode_roundtrip (input in ".*" ) { let encoded = encode (&input); let decoded = decode (&encoded). unwrap (); assert_eq! (input, decoded); } #[test] fn sort_preserves_length ( mut vec in prop::collection:: vec (any::< i32 >(), 0 .. 100 )) { let original_len = vec. len (); vec. sort (); assert_eq! (vec. len (), original_len); } #[test] fn sort_produces_ordered_output ( mut vec in prop::collection:: vec (any::< i32 >(), 0 .. 100 )) { vec. sort (); for window in vec. windows ( 2 ) { assert! (window[ 0 ] <= window[ 1 ]); } } } Özel Stratejiler use proptest::prelude::*; fn valid_email () -> impl Strategy <Value = String > { ( "[a-z]{1,10}" , "[a-z]{1,5}" ) . prop_map (|(user, domain)| format! ( "{user}@{domain}.com" )) } proptest! { #[test] fn accepts_valid_emails (email in valid_email ()) { assert! (User:: new ( "Test" , &email). is_ok ()); } } mockall ile Mock'lama Trait-Tabanlı Mock'lama use mockall::{automock, predicate::eq}; #[automock] trait UserRepository { fn find_by_id (& self , id: u64 ) -> Option <User>; fn save (& self , user: &User) -> Result <(), StorageError>; } #[test] fn service_returns_user_when_found () { let mut mock = MockUserRepository:: new (); mock. expect_find_by_id () . with ( eq ( 42 )) . times ( 1 ) . returning (|_| Some (User { id: 42 , name: "Alice" . into () })); let service = UserService:: new ( Box :: new (mock)); let user = service. get_user ( 42 ). unwrap (); assert_eq! (user.name, "Alice" ); } #[test] fn service_returns_none_when_not_found () { let mut mock = MockUserRepository:: new (); mock. expect_find_by_id () . returning (|_| None ); let service = UserService:: new ( Box :: new (mock)); assert! (service. get_user ( 99 ). is_none ()); } Doc Testleri Çalıştırılabilir Dokümantasyon /// İki sayıyı toplar. /// /// # Examples /// /// ``` /// use my_crate::add; /// /// assert_eq!(add(2, 3), 5); /// assert_eq!(add(-1, 1), 0); /// ``` pub fn add (a: i32 , b: i32 ) -> i32 { a + b } /// Bir config string'i parse eder. /// /// # Errors /// /// Girdi geçerli TOML değilse `Err` döner. /// /// ```no_run /// use my_crate::parse_config; /// /// let config = parse_config(r#"port = 8080"#).unwrap(); /// assert_eq!(config.port, 8080); /// ``` /// /// ```no_run /// use my_crate::parse_config; ///
Agent 识别该技能的关键词,点击任意一个即可复制。
该技能未提供触发词。
下载的 .skill 包内含以下字段。
| 字段 | 说明 |
|---|---|
| format | 格式标识(skill/v1) |
| skill_id | 技能唯一 ID |
| name | 技能名称 |
| version | 版本号 |
| description | 技能描述 |
| category | 所属分类(数组) |
| trigger_words | 触发词列表 |
| tags | 标签列表 |
| source | 来源标识 |
| source_url | 来源链接(本页地址) |
| exported_at | 导出时间(每次下载生成) |
| system_prompt | 系统提示词正文 |
| model_config | 模型参数:provider / model / temperature / max_tokens / top_p |
| examples | 示例 |
| install_guide | 各平台导入说明(Coze / Dify / Claude / 自定义框架) |