mit einem Klick
test-driven-development
Follow TDD practices with Red-Green-Refactor cycle. Write tests before implementation, use AAA pattern, and ensure high-quality test coverage. Use when implementing features or fixing bugs.
Menü
Follow TDD practices with Red-Green-Refactor cycle. Write tests before implementation, use AAA pattern, and ensure high-quality test coverage. Use when implementing features or fixing bugs.
Apply DDD tactical patterns (Entities, Value Objects, Aggregates, Domain Services, Repositories) and strategic design (Ubiquitous Language, Bounded Contexts). Use when modeling complex business logic.
Apply Clean Architecture layering and dependency rules (Domain, Application, Infrastructure, Presentation layers). Use when structuring applications or ensuring dependencies point inward.
Write clear, testable requirements using User Stories and Gherkin scenarios. Capture functional and non-functional requirements with proper acceptance criteria. Use when defining new features or documenting system behavior.
Enforce SOLID principles (Single Responsibility, Open/Closed, Liskov Substitution, Interface Segregation, Dependency Inversion) in object-oriented design. Use when writing or reviewing classes and modules.
Follow trunk-based development practices with short-lived branches, frequent integration to main, feature flags, and continuous integration. Use when managing git workflow and releases.
| name | test-driven-development |
| description | Follow TDD practices with Red-Green-Refactor cycle. Write tests before implementation, use AAA pattern, and ensure high-quality test coverage. Use when implementing features or fixing bugs. |
You are assisting with code that must be developed using Test-Driven Development practices.
Repeat: Start the cycle again for the next small piece of functionality
# WRONG: Writing implementation first
def calculate_competence_points(grades):
# implementation here
pass
# RIGHT: Writing test first
def test_calculate_competence_points_with_all_passing_grades():
grades = [Grade("Math", 5), Grade("Norwegian", 4)]
points = calculate_competence_points(grades)
assert points == 45 # Then write implementation
# GOOD: Tests behavior
def test_student_is_qualified_when_meeting_minimum_grade():
student = Student("John", grades=[Grade("Math", 4)])
rule = MinimumGradeRule(threshold=4)
assert rule.evaluate(student) == True
# BAD: Tests implementation details
def test_rule_calls_grade_comparator_method():
# Testing internal methods/implementation
pass
def test_quota_rule_rejects_when_quota_full():
# ARRANGE - Set up test data
quota = Quota(name="Engineering", capacity=100, filled=100)
student = Student("Jane")
rule = QuotaRule(quota)
# ACT - Execute the behavior
result = rule.evaluate(student)
# ASSERT - Verify outcome
assert result.is_rejected
assert result.reason == "Quota full"
Use descriptive names that explain:
Format: test_<what>_<condition>_<expected>
Examples:
test_admission_rule_passes_when_grades_meet_minimumtest_quota_calculation_raises_error_when_capacity_negativetest_student_competence_points_equals_zero_for_empty_gradesF - Fast: Tests should run quickly I - Independent: Tests should not depend on each other R - Repeatable: Same input = same output, every time S - Self-validating: Pass or fail, no manual checking T - Timely: Written just before production code
tests/
├── unit/
│ ├── domain/
│ │ ├── test_admission_rule.py
│ │ └── test_student.py
│ ├── application/
│ │ └── test_evaluate_admission_use_case.py
│ └── infrastructure/
│ └── test_rule_repository.py
├── integration/
│ └── test_admission_workflow.py
└── e2e/
└── test_complete_admission_process.py
Dummy: Placeholder, never used
dummy_logger = Mock(spec=Logger)
Stub: Returns predefined data
class StubRuleRepository:
def get_rules(self):
return [PresetRule1(), PresetRule2()]
Mock: Verifies interactions
mock_gateway = Mock()
use_case.execute(student_id)
mock_gateway.notify_student.assert_called_once()
Fake: Working implementation (simpler)
class FakeRuleRepository:
def __init__(self):
self._rules = {}
def add(self, rule):
self._rules[rule.id] = rule
@pytest.fixture
def sample_student():
return Student(
name="John Doe",
grades=[Grade("Math", 5), Grade("Norwegian", 4)]
)
def test_with_fixture(sample_student):
assert sample_student.name == "John Doe"
@pytest.mark.parametrize("grade,expected_points", [
(6, 24),
(5, 20),
(4, 16),
(3, 12),
])
def test_grade_to_points_conversion(grade, expected_points):
assert convert_grade_to_points(grade) == expected_points
test_minimum_grade_rule_passes_when_grade_meets_thresholdMinimumGradeRule.evaluate() methodWhen practicing TDD: