$ loading_
帮助你系统掌握 Rust 测试模式与 TDD 实践,提升代码质量与可维护性。
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "rust-testing" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/rust-testing/SKILL.md 2. 保存为 ~/.claude/skills/rust-testing/SKILL.md 3. 装好后重载技能,告诉我可以用了
请为这个 Rust 模块设计并编写单元测试,覆盖正常路径、边界条件和错误处理,并解释每个测试的目的。
一组结构清晰的 Rust 单元测试代码,并附带覆盖点与测试意图说明。
请使用 Rust 异步测试模式为以下 async 函数编写测试,包含成功、超时与异常场景,并说明需要的测试运行时配置。
可运行的异步测试示例,包含测试场景划分及相关运行时配置建议。
请基于 TDD 方法,为这段 Rust 代码先列出测试用例,再给出重构建议,补充 mock、集成测试和覆盖率提升方案。
按 TDD 步骤组织的测试计划、重构建议,以及覆盖率提升实施方案。
Comprehensive Rust testing patterns for writing reliable, maintainable tests following TDD methodology.
#[test] in a #[cfg(test)] module, rstest for parameterized tests, or proptest for property-based testsRED → Write a failing test first
GREEN → Write minimal code to pass the test
REFACTOR → Improve code while keeping tests green
REPEAT → Continue with next requirement
// RED: Write test first, use todo!() as placeholder
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 → panics at 'not yet implemented'
// GREEN: Replace todo!() with minimal implementation
pub fn add(a: i32, b: i32) -> i32 { a + b }
// cargo test → PASS, then REFACTOR while keeping tests green
// 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", "[email protected]").unwrap();
assert_eq!(user.display_name(), "Alice");
assert_eq!(user.email, "[email protected]");
}
#[test]
fn rejects_invalid_email() {
let result = User::new("Bob", "not-an-email");
assert!(result.is_err());
assert!(result.unwrap_err().contains("invalid email"));
}
}
assert_eq!(2 + 2, 4); // Equality
assert_ne!(2 + 2, 5); // Inequality
assert!(vec![1, 2, 3].contains(&2)); // Boolean
assert_eq!(value, 42, "expected 42 but got {value}"); // Custom message
assert!((0.1_f64 + 0.2 - 0.3).abs() < f64::EPSILON); // Float comparison
Result Returns#[test]
fn parse_returns_error_for_invalid_input() {
let result = parse_config("}{invalid");
assert!(result.is_err());
// Assert specific error variant
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(()) // Test fails if any ? returns Err
}
#[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];
}
my_crate/
├── src/
│ └── lib.rs
├── tests/ # Integration tests
…
通过双评审智能体对结果进行对抗式校验,提升输出发布前的可靠性
帮助开发者用 Kotest、MockK 与协程测试构建高质量 Kotlin 测试体系。