tdd workflow
npx skills add https://github.com/shunsukehayashi/miyabi-claude-plugins --skill TDD Workflow
Skill 文档
Test-Driven Development (TDD) Workflow
Version: 1.0.0 Last Updated: 2025-11-26 Priority: P0 Level Purpose: ãã¹ãé§åéçºã«ããé«å質ãªã³ã¼ãå®è£
æ¦è¦
Miyabiããã¸ã§ã¯ãã«ãããTDDï¼Test-Driven Developmentï¼ã®å®å ¨ãªã¯ã¼ã¯ããã¼ã Red-Green-Refactorãµã¤ã¯ã«ãéãã¦ãå ç¢ã§ä¿å®æ§ã®é«ãã³ã¼ããå®ç¾ãã¾ãã
å¼ã³åºãããªã¬ã¼
| ããªã¬ã¼ | ä¾ |
|---|---|
| æ°æ©è½å®è£ | “implement feature X”, “add new functionality” |
| ãã°ä¿®æ£ | “fix bug”, “resolve issue” |
| ãã¹ã追å | “add tests”, “write tests for” |
| ãªãã¡ã¯ã¿ãªã³ã° | “refactor with tests”, “improve code” |
| TDDä¾é ¼ | “use TDD”, “test-driven” |
TDDãµã¤ã¯ã«: Red-Green-Refactor
graph LR
RED[RED<br/>失æãããã¹ã使] --> GREEN[GREEN<br/>æå°å®è£
ã§æå]
GREEN --> REFACTOR[REFACTOR<br/>ã³ã¼ãæ¹å]
REFACTOR --> RED
style RED fill:#ff6b6b,stroke:#333,color:#fff
style GREEN fill:#51cf66,stroke:#333,color:#fff
style REFACTOR fill:#339af0,stroke:#333,color:#fff
Phase 1: RED (失æãããã¹ããæ¸ã)
ç®ç: å®è£ ãããæ©è½ã®ä»æ§ããã¹ãã¨ãã¦ææå
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_new_feature_basic() {
// Arrange: ãã¹ãæºå
let input = "test_input";
// Act: å®è¡
let result = new_feature(input);
// Assert: æ¤è¨¼
assert_eq!(result, expected_output);
}
#[test]
fn test_new_feature_edge_case() {
// ã¨ãã¸ã±ã¼ã¹ã®ãã¹ã
let result = new_feature("");
assert!(result.is_err());
}
}
å®è¡:
cargo test test_new_feature --no-run && cargo test test_new_feature
# æå¾
: FAILED (ãã¹ãã失æãããã¨ã確èª)
Phase 2: GREEN (ãã¹ããéãæå°å®è£ )
ç®ç: ãã¹ããéãããã®æå°éã®ã³ã¼ãå®è£
pub fn new_feature(input: &str) -> Result<String, Error> {
// æå°éã®å®è£
if input.is_empty() {
return Err(Error::InvalidInput);
}
Ok(expected_output.to_string())
}
å®è¡:
cargo test test_new_feature
# æå¾
: PASSED (å
¨ãã¹ãæå)
Phase 3: REFACTOR (ã³ã¼ãæ¹å)
ç®ç: ãã¹ããç¶æããªããã³ã¼ãåè³ªãæ¹å
pub fn new_feature(input: &str) -> Result<String, Error> {
// ããªãã¼ã·ã§ã³åé¢
validate_input(input)?;
// å¦çã®æç¢ºå
let processed = process_input(input);
// çµææ§ç¯
Ok(build_output(processed))
}
fn validate_input(input: &str) -> Result<(), Error> {
if input.is_empty() {
return Err(Error::InvalidInput);
}
Ok(())
}
å®è¡:
cargo test test_new_feature && cargo clippy && cargo fmt -- --check
# æå¾
: å
¨ãã¹ãæå + è¦åãªã + ãã©ã¼ãããæ¸ã¿
ãã¹ããã©ããã
/\
/ \ E2E Tests (10%)
/----\ - å®å
¨ãªã¦ã¼ã¶ã¼ããã¼
/ \ - æ¬çªç°å¢ã«è¿ãç¶æ
/--------\
/ \ Integration Tests (30%)
/------------\ - ã³ã³ãã¼ãã³ãé飿º
/ \ - å¤é¨API模æ¬
/----------------\
/ \ Unit Tests (60%)
/--------------------\ - 颿°ã»ã¡ã½ããåä½
- é«éã»ç¬ç«ã»æ±ºå®ç
åã¬ãã«ã®ç®å®
| ã¬ãã« | ã«ãã¬ãã¸ç®æ¨ | å®è¡æé | é »åº¦ |
|---|---|---|---|
| Unit | 80%+ | < 1ç§/ãã¹ã | 常æ |
| Integration | Key paths | < 10ç§/ãã¹ã | CI |
| E2E | Critical flows | < 60ç§/ãã¹ã | æ¥æ¬¡ |
Rust TDD ãã¿ã¼ã³é
Pattern 1: Resultåã®ãã¹ã
#[test]
fn test_operation_success() {
let result = operation(valid_input);
assert!(result.is_ok());
assert_eq!(result.unwrap(), expected);
}
#[test]
fn test_operation_error() {
let result = operation(invalid_input);
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), Error::InvalidInput));
}
Pattern 2: async颿°ã®ãã¹ã
#[tokio::test]
async fn test_async_operation() {
let result = async_operation().await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_async_with_timeout() {
let result = tokio::time::timeout(
Duration::from_secs(5),
async_operation()
).await;
assert!(result.is_ok());
}
Pattern 3: ã¢ãã¯ä½¿ç¨
use mockall::predicate::*;
use mockall::mock;
mock! {
pub ExternalService {
async fn call(&self, input: &str) -> Result<String, Error>;
}
}
#[tokio::test]
async fn test_with_mock() {
let mut mock = MockExternalService::new();
mock.expect_call()
.with(eq("test"))
.returning(|_| Ok("response".to_string()));
let result = my_function(&mock).await;
assert!(result.is_ok());
}
Pattern 4: ããããã£ãã¼ã¹ãã¹ã
use proptest::prelude::*;
proptest! {
#[test]
fn test_property(input in ".*") {
let result = process(&input);
// ä»»æã®å
¥åã«å¯¾ãã¦å¸¸ã«çãªæ§è³ª
prop_assert!(result.len() >= 0);
}
#[test]
fn test_roundtrip(input in any::<u32>()) {
let encoded = encode(input);
let decoded = decode(&encoded);
prop_assert_eq!(input, decoded);
}
}
Pattern 5: ãã¹ããã£ã¯ã¹ãã£
struct TestFixture {
db: TestDatabase,
service: TestService,
}
impl TestFixture {
async fn new() -> Self {
let db = TestDatabase::setup().await;
let service = TestService::new(&db);
Self { db, service }
}
async fn teardown(self) {
self.db.cleanup().await;
}
}
#[tokio::test]
async fn test_with_fixture() {
let fixture = TestFixture::new().await;
// ãã¹ãå®è¡
let result = fixture.service.operation().await;
assert!(result.is_ok());
fixture.teardown().await;
}
Pattern 6: ã¹ãããã·ã§ãããã¹ã
use insta::assert_snapshot;
#[test]
fn test_output_format() {
let result = generate_output(input);
assert_snapshot!(result);
}
#[test]
fn test_json_output() {
let result = to_json(&data);
assert_snapshot!(result);
}
ã³ãã³ããªãã¡ã¬ã³ã¹
åºæ¬ãã¹ãã³ãã³ã
# å
¨ãã¹ãå®è¡
cargo test --workspace
# ç¹å®ããã±ã¼ã¸ã®ãã¹ã
cargo test -p miyabi-agents
# ç¹å®ãã¹ãã®å®è¡
cargo test test_name
# ãã¹ãåã®ãã¿ã¼ã³ããã
cargo test workflow_
# 並å度å¶å¾¡
cargo test -- --test-threads=1
# åºå表示
cargo test -- --nocapture
# 失ææã®ã¿åºå
cargo test -- --show-output
é«åº¦ãªãã¹ãã³ãã³ã
# ããã¥ã¡ã³ããã¹ã
cargo test --doc
# çµ±åãã¹ãã®ã¿
cargo test --test integration_test
# ãã³ããã¼ã¯
cargo bench
# ã«ãã¬ã㸠(cargo-llvm-cov)
cargo llvm-cov --workspace --html
# ããããã£ãã¹ã(é·æé)
PROPTEST_CASES=10000 cargo test
CIç¨ã³ãã³ã
# ãã«ãã§ãã¯
cargo test --workspace --all-features && \
cargo clippy --workspace --all-targets --all-features -- -D warnings && \
cargo fmt --all -- --check
# ã«ãã¬ãã¸ã¬ãã¼ã
cargo llvm-cov --workspace --lcov --output-path lcov.info
ãã¹ãæ§é
ãã£ã¬ã¯ããªæ§æ
crates/miyabi-xxx/
âââ src/
â âââ lib.rs
â âââ feature.rs # æ©è½ã³ã¼ã
âââ tests/
â âââ integration_test.rs # çµ±åãã¹ã
â âââ e2e_test.rs # E2Eãã¹ã
âââ benches/
âââ benchmark.rs # ãã³ããã¼ã¯
ãã¹ãã¢ã¸ã¥ã¼ã«æ§æ
// src/feature.rs
pub fn feature_function() -> Result<(), Error> {
// å®è£
}
#[cfg(test)]
mod tests {
use super::*;
mod success_cases {
use super::*;
#[test]
fn test_basic_success() { }
#[test]
fn test_with_options() { }
}
mod error_cases {
use super::*;
#[test]
fn test_invalid_input() { }
#[test]
fn test_network_error() { }
}
mod edge_cases {
use super::*;
#[test]
fn test_empty_input() { }
#[test]
fn test_max_size() { }
}
}
ãã¹ããã©ã¯ãã£ã¹
DO (æ¨å¥¨)
-
1ãã¹ã1ã¢ãµã¼ã·ã§ã³åå
#[test] fn test_single_assertion() { let result = operation(); assert_eq!(result, expected); } -
AAA ãã¿ã¼ã³ (Arrange-Act-Assert)
#[test] fn test_aaa_pattern() { // Arrange let input = setup_input(); // Act let result = operation(input); // Assert assert!(result.is_ok()); } -
æå³ã表ããã¹ãå
#[test] fn when_input_is_empty_returns_validation_error() { } #[test] fn given_valid_user_when_login_then_returns_token() { } -
ãã¹ããã¼ã¿ã®æç¤ºå
#[test] fn test_with_explicit_data() { let user = User { id: 1, name: "Test User".to_string(), email: "test@example.com".to_string(), }; // ... }
DON’T (鿍奍)
-
ãã¹ãéã®ä¾å
// BAD: ãã¹ãã®å®è¡é åºã«ä¾å static mut SHARED_STATE: i32 = 0; -
å®è£ 詳細ã®ãã¹ã
// BAD: å 鍿§é ã«ä¾å assert_eq!(result.internal_cache.len(), 5); -
éæ±ºå®çãã¹ã
// BAD: æéä¾å assert!(Instant::now() > start_time); -
é度ã«è¤éãªã»ããã¢ãã
// BAD: 50è¡ã®ã»ããã¢ããã³ã¼ã
ã«ãã¬ãã¸ã¬ã¤ãã©ã¤ã³
ã«ãã¬ãã¸ç®æ¨
| ã³ã³ãã¼ãã³ã | ç®æ¨ | åªå 度 |
|---|---|---|
| Core Types | 90%+ | P0 |
| Business Logic | 85%+ | P0 |
| API Handlers | 80%+ | P1 |
| Utilities | 70%+ | P2 |
| CLI | 60%+ | P2 |
ã«ãã¬ãã¸æ¸¬å®
# ã¤ã³ã¹ãã¼ã«
cargo install cargo-llvm-cov
# 測å®å®è¡
cargo llvm-cov --workspace --html
# ã¬ãã¼ã確èª
open target/llvm-cov/html/index.html
ã«ãã¬ãã¸é¤å¤
// ã«ãã¬ãã¸ããé¤å¤
#[cfg(not(tarpaulin_include))]
fn debug_only_function() { }
// ã¾ãã¯
#[coverage(off)]
fn uncoverable_function() { }
CI/CDçµ±å
GitHub Actionsè¨å®
name: TDD Workflow
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup Rust
uses: dtolnay/rust-action@stable
- name: Run Tests
run: |
cargo test --workspace --all-features
- name: Check Clippy
run: |
cargo clippy --workspace --all-targets -- -D warnings
- name: Check Format
run: |
cargo fmt --all -- --check
- name: Coverage
run: |
cargo llvm-cov --workspace --lcov --output-path lcov.info
- name: Upload Coverage
uses: codecov/codecov-action@v3
with:
files: lcov.info
ãã©ãã«ã·ã¥ã¼ãã£ã³ã°
ãã¹ããä¸å®å®
çç¶: åããã¹ããæã 失æãã
対å¦:
# 並åå®è¡ãç¡å¹å
cargo test -- --test-threads=1
# 詳細ãã°
RUST_LOG=debug cargo test
# ç¹å®ãã¹ãã100åå®è¡
for i in {1..100}; do cargo test flaky_test || exit 1; done
ãã¹ããé ã
çç¶: ãã¹ãå®è¡ã«æéãããã
対å¦:
# ã³ã³ãã¤ã«æéã®ç¢ºèª
cargo build --timings
# ãã¹ãæéã®è¨æ¸¬
cargo test -- -Z unstable-options --report-time
# 並å度調æ´
cargo test -- --test-threads=8
ã¢ãã¯ãåä½ããªã
çç¶: ã¢ãã¯ãå¼ã³åºãããªã
対å¦:
// expect_*ã®ãã§ãã¯
mock.checkpoint(); // æå¾
ããå¼ã³åºããæ¤è¨¼
// ããå
·ä½çãªãããã£ã¼
mock.expect_call()
.with(eq("specific_input"))
.times(1)
.returning(|_| Ok(()));
æååºæº
| ãã§ãã¯é ç® | åºæº |
|---|---|
| Unit Tests | 100% pass |
| Integration Tests | 100% pass |
| Coverage | > 80% |
| No Warnings | cargo clippy clean |
| Formatted | cargo fmt clean |
åºåãã©ã¼ããã
TDD Workflow Results
RED Phase:
New tests written: 5
Tests failing: 5 (expected)
GREEN Phase:
Implementation: Complete
Tests passing: 5/5
REFACTOR Phase:
Code improved: Yes
Tests still passing: 5/5
Coverage: 87.3%
Clippy: 0 warnings
Format: Clean
Ready to commit
é¢é£ããã¥ã¡ã³ã
| ããã¥ã¡ã³ã | ç¨é |
|---|---|
context/rust.md |
Rustéçºã¬ã¤ãã©ã¤ã³ |
Skills/rust-development/ |
Rustãã«ãã¯ã¼ã¯ããã¼ |
Skills/debugging-troubleshooting/ |
ãããã°æ¯æ´ |
é¢é£Skills
- Rust Development: ãã«ãã»ãã¹ãå®è¡
- Debugging Troubleshooting: ãã¹ã失ææã®ãããã°
- Git Workflow: ã³ãããåã®ãã¹ã確èª
- Security Audit: ã»ãã¥ãªãã£ãã¹ã