rust-testing

Solid

Rust测试模式,包括单元测试、集成测试、异步测试、基于属性的测试、模拟和覆盖率。遵循TDD方法学。

Testing & QA 201,447 stars 30903 forks Updated yesterday MIT

Install

View on GitHub

Quality Score: 95/100

Stars 20%
100
Recency 20%
100
Frontmatter 20%
70
Documentation 15%
100
Issue Health 10%
50
License 10%
100
Description 5%
100

Skill Content

# Rust 测试模式 遵循 TDD 方法论编写可靠、可维护测试的全面 Rust 测试模式。 ## 何时使用 * 编写新的 Rust 函数、方法或特征 * 为现有代码添加测试覆盖率 * 为性能关键代码创建基准测试 * 为输入验证实现基于属性的测试 * 在 Rust 项目中遵循 TDD 工作流 ## 工作原理 1. **识别目标代码** — 找到要测试的函数、特征或模块 2. **编写测试** — 在 `#[cfg(test)]` 模块中使用 `#[test]`,使用 rstest 进行参数化测试,或使用 proptest 进行基于属性的测试 3. **模拟依赖项** — 使用 mockall 来隔离被测单元 4. **运行测试 (RED)** — 验证测试是否按预期失败 5. **实现 (GREEN)** — 编写最少代码以通过测试 6. **重构** — 改进代码同时保持测试通过 7. **检查覆盖率** — 使用 cargo-llvm-cov,目标 80% 以上 ## Rust 的 TDD 工作流 ### RED-GREEN-REFACTOR 循环 ``` RED → 先写一个失败的测试 GREEN → 编写最少代码使测试通过 REFACTOR → 重构代码,同时保持测试通过 REPEAT → 继续下一个需求 ``` ### Rust 中的分步 TDD ```rust // 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' ``` ```rust // 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 ``` ## 单元测试 ### 模块级测试组织 ```rust // 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 { &s...

Details

Author
affaan-m
Repository
affaan-m/everything-claude-code
Created
4 months ago
Last Updated
yesterday
Language
JavaScript
License
MIT

Integrates with

Similar Skills

Semantically similar based on skill content — not just same category

Testing & QA Listed

rust-testing

Rust测试模式,包括单元测试、集成测试、异步测试、基于属性的测试、模拟和覆盖率。遵循TDD方法学。

0 Updated 2 months ago
SakulaPor
Testing & QA Listed

rust-testing

Rust testing patterns including unit tests, integration tests, async testing, property-based testing, mocking, and coverage. Follows TDD methodology.

2 Updated today
caovinhphuc
Testing & QA Featured

rtk-tdd

Enforces TDD (Red-Green-Refactor) for Rust development. Auto-triggers on implementation, testing, refactoring, and bug fixing tasks. Provides Rust-idiomatic testing patterns with anyhow/thiserror, cfg(test), and Arrange-Act-Assert workflow.

57,145 Updated yesterday
rtk-ai
Testing & QA Listed

unit-test

Project-aware unit testing guide with mock patterns, TDD discipline, and best practices. Auto-detects test framework (Jest/Vitest) and provides appropriate examples. Use when: (1) writing or fixing unit tests, (2) user says "寫測試", "write test", "補測試", "add test", (3) user encounters mock patterns or test failures, (4) user asks how to test a composable, component, or store, (5) user says "mock imports", "test store", "怎麼測", "測試怎麼寫", (6) reviewing test quality: "unit test review", "review tests", "check test quality", "測試審查", "review 測試", "測試品質", "檢查測試品質", (7) TDD workflow: "TDD", "test driven", "紅綠燈", "先寫測試", "test first", "red green refactor".

4 Updated today
HsuanYuLee
Testing & QA Listed

testing

Testing strategies and methodologies including TDD and E2E testing. Use when writing tests, implementing TDD workflow, or setting up E2E test infrastructure.

42 Updated 1 months ago
xiaobei930