skills/rust-testing/SKILL.md
Rust 測試策略。用於單元測試、整合測試、mock、property-based testing、test utilities、benchmark。
npx skillsauth add vincent119/ai-rules-kit rust-testingInstall this skill globally with one command. Works with Claude Code, Cursor, and Windsurf.
3 of 9 scanners reported clean
Some scanners were skipped, did not run, or reported a non-clean status. Review each row below.
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_valid_input() {
let result = parse("42");
assert_eq!(result, Ok(42));
}
#[test]
fn parse_invalid_input() {
let result = parse("abc");
assert!(result.is_err());
}
#[test]
#[should_panic(expected = "index out of bounds")]
fn panics_on_invalid_index() {
let v: Vec<i32> = vec![];
let _ = v[0];
}
}
tests/
├── integration_test.rs
└── common/
└── mod.rs # 共用 test utilities
// tests/integration_test.rs
mod common;
#[test]
fn full_workflow() {
let client = common::setup_test_client();
let result = client.create_user("[email protected]");
assert!(result.is_ok());
}
use mockall::automock;
#[automock]
pub trait UserRepository {
fn find_by_id(&self, id: &str) -> Option<User>;
fn save(&self, user: &User) -> Result<(), RepositoryError>;
}
#[cfg(test)]
mod tests {
use super::*;
use mockall::predicate::*;
#[test]
fn service_returns_user() {
let mut mock_repo = MockUserRepository::new();
mock_repo
.expect_find_by_id()
.with(eq("user-1"))
.returning(|_| Some(User { id: "user-1".into(), name: "Alice".into() }));
let service = UserService::new(mock_repo);
let user = service.get_user("user-1");
assert_eq!(user.unwrap().name, "Alice");
}
}
/// 抽象 I/O 操作為 trait,方便測試
pub trait FileSystem {
fn read_to_string(&self, path: &str) -> std::io::Result<String>;
fn write(&self, path: &str, content: &[u8]) -> std::io::Result<()>;
}
pub struct RealFs;
impl FileSystem for RealFs {
fn read_to_string(&self, path: &str) -> std::io::Result<String> {
std::fs::read_to_string(path)
}
fn write(&self, path: &str, content: &[u8]) -> std::io::Result<()> {
std::fs::write(path, content)
}
}
# Cargo.toml
[features]
test-util = []
#[cfg(feature = "test-util")]
pub mod test_helpers {
pub fn create_test_config() -> Config {
Config { host: "localhost".into(), port: 0 }
}
}
use proptest::prelude::*;
proptest! {
#[test]
fn roundtrip_serialization(input in "\\PC{1,100}") {
let encoded = encode(&input);
let decoded = decode(&encoded).unwrap();
prop_assert_eq!(input, decoded);
}
}
#[tokio::test]
async fn async_operation_succeeds() {
let result = fetch_data("https://example.com").await;
assert!(result.is_ok());
}
// benches/my_benchmark.rs
use criterion::{criterion_group, criterion_main, Criterion};
fn bench_parse(c: &mut Criterion) {
c.bench_function("parse_config", |b| {
b.iter(|| parse_config(include_str!("../fixtures/config.toml")))
});
}
criterion_group!(benches, bench_parse);
criterion_main!(benches);
tools
基於 SLA/SLO 量化評估事故影響的計算模型與業務影響矩陣。適用於「SLA 影響」、「SLO 違反」、「影響評估」、「營收損失估算」、「Error Budget」、「可用性計算」、「事故成本評估」等量化事故業務影響的任務。強化 impact-assessor 的評估能力。注意:事故原因分析與改善規劃不在此技能範圍內。
research
根因分析(RCA)方法論詳細指南。提供 5 Whys、Fishbone 圖、Fault Tree Analysis、變更分析等結構化 RCA 技術,以及認知偏誤防範清單。適用於「根因分析」、「RCA」、「5 Whys」、「魚骨圖」、「Fault Tree」、「原因分析方法論」、「變更分析」等事故原因分析任務。強化 root-cause-investigator 的分析能力。注意:時間軸重建與改善規劃不在此技能範圍內。
testing
事故事後分析(Postmortem)完整流程。協調 7 個執行階段:資訊收集 → 時間軸重建 → 根因分析 → 影響評估 → 改善規劃 → 報告審查 → 整合報告,最終產出完整的 Postmortem 報告。適用於「寫事故報告」、「post-incident 分析」、「RCA 報告」、「事故時間軸整理」、「建立改善措施」等請求。注意:即時 Incident Response(on-call)、監控系統設定、告警配置不在此技能範圍內。
content-media
投影片版面模式庫。提供 20 種投影片類型的最佳版面配置、格線系統、色彩與字型設計 Token。適用於「投影片版面」、「Slide Layout」、「設計系統」、「格線」、「字型」、「色彩規範」等投影片視覺設計任務。強化 visual-designer 的設計能力。注意:PPT/Keynote 檔案直接輸出不在此技能範圍內。