trax/tests/.cursor/rules/tdd.mdc

118 lines
3.8 KiB
Plaintext

---
description: Test-Driven Development Rules for automated testing and code quality for tests/
alwaysApply: false
---
# Test-Driven Development Rule
## Core Principles
- **Tests First**: Write tests before implementing functionality
- **Complete Coverage**: Tests should cover all requirements
- **Automated Verification**: All tests must be automated
- **Quality Gate**: Code must pass all tests before merging and edge cases
- **Edge Case Coverage**: Tests should cover all edge cases
- **Error Handling**: Tests should cover error handling and edge cases
- **Mocking**: Tests should use mocks for external dependencies
- **Test Data**: Tests should use test data for input and output
- **Test Fixtures**: Tests should use test fixtures for setup and teardown
- **Test Coverage**: Tests should cover all requirements and edge cases
- **Test Performance**: Tests should be fast and efficient
- **Test Reliability**: Tests should be reliable and consistent
## Implementation Patterns
### Test-First Development
```python
# ✅ DO: Write the test before implementing the feature
# test_user_service.py
def test_create_user_success():
# Arrange
user_service = UserService()
user_data = {"name": "Test User", "email": "test@example.com"}
# Act
result = user_service.create_user(user_data)
# Assert
assert result.success is True
assert result.user.name == "Test User"
assert result.user.email == "test@example.com"
# THEN implement the feature to make the test pass
```
### Test Coverage
```python
# ✅ DO: Test both success and failure cases
def test_create_user_invalid_email():
# Arrange
user_service = UserService()
user_data = {"name": "Test User", "email": "invalid-email"}
# Act
result = user_service.create_user(user_data)
# Assert
assert result.success is False
assert "Invalid email format" in result.error_message
```
### Edge Case Coverage
```python
# ✅ DO: Test edge cases
def test_create_user_edge_case():
# Arrange
user_service = UserService()
user_data = {"name": "Test User", "email": "test@example.com"}
# Act
result = user_service.create_user(user_data)
# Assert
assert result.success is True
assert result.user.name == "Test User"
assert result.user.email == "test@example.com"
```
### Error Handling
```python
# ✅ DO: Test error handling
def test_create_user_error_handling():
# Arrange
user_service = UserService()
user_data = {"name": "Test User", "email": "invalid-email"}
# Act
result = user_service.create_user(user_data)
# Assert
assert result.success is True
assert result.user.name == "Test User"
assert result.user.email == "test@example.com"
```
### Anti-Patterns Implement code without tests
```python
# ❌ DON'T: Implement code without tests
def create_user(user_data):
# Implementation without corresponding tests
# This makes it difficult to verify behavior and prevent regressions
pass
```
### Anti-Patterns
```python
# ❌ DON'T: Write tests after implementation
# This often leads to tests that validate the implementation rather than the requirements
```
BEFORE writing any code or starting a new feature:
1. **ALWAYS validate task context** using the CLI:
```bash
task-master show <task-id>
```
2. **ALWAYS design and implement thorough, automated unit tests** that precisely capture all requirements. If any requirement is ambiguous or incomplete, seek clarification before proceeding with implementation. All unit tests must be automated and integrated into the continuous integration pipeline. No functional code may be merged unless it achieves a 100% unit test pass rate, except where a documented exception has been explicitly approved by a manager with clear justification.