Skip to content

TDD with Specifications

Status: Placeholder - Content coming soon

Prerequisites: Your First Specification, Your First Module

Planned Content

This tutorial teaches the complete test-driven development loop using executable Gherkin specifications with Godog.

What You'll Learn

  • Start with a feature idea and write a Gherkin specification
  • Generate step definition scaffolding
  • Implement step definitions that connect specs to code
  • Follow the TDD cycle: Write failing test → Implement → Tests pass
  • Run specifications with r2r test suite acceptance
  • Understand the relationship between specifications and unit tests

Tutorial Structure

  1. Choose a feature to implement
  2. Example: "Calculate tax on invoice"
  3. Define the business rules
  4. Identify edge cases

  5. Write the Gherkin specification

  6. Create specs/billing/calculate-tax/specification.feature
  7. Define Feature, Rules, and Scenarios
  8. Use concrete examples with real values
  9. Tag appropriately (@L2 @ov @deps:go)

  10. Generate step definition scaffolding

  11. Run the specification to get undefined steps
  12. Copy generated step signatures
  13. Create go/billing/tests/tax_steps.go

  14. Implement step definitions (RED)

  15. Wire Gherkin steps to test code
  16. Set up test context and state
  17. Assert expected outcomes
  18. Steps fail because implementation doesn't exist

  19. Write unit tests (RED)

  20. Create go/billing/tax_test.go
  21. Write table-driven tests for tax calculation
  22. Tests fail because implementation doesn't exist

  23. Implement the feature (GREEN)

  24. Create go/billing/tax.go
  25. Implement tax calculation logic
  26. Run tests: r2r test billing
  27. All tests pass

  28. Refactor and validate

  29. Improve code clarity
  30. Extract helper functions
  31. Run full test suite
  32. Run specification: r2r test suite acceptance

Complete Example

The tutorial will implement a tax calculation feature end-to-end:

Specification:

@L2 @ov @deps:go
Feature: billing_calculate-tax

  As a billing system
  I want to calculate tax on invoices
  So that customers are charged correctly

  Rule: Standard tax rate is 20%

    Scenario: Calculate tax on taxable invoice
      Given an invoice with subtotal $100.00
      When I calculate tax
      Then the tax amount should be $20.00
      And the total should be $120.00

Step Definitions:

func (s *TaxContext) anInvoiceWithSubtotal(amount string) error {
    s.invoice = &Invoice{Subtotal: parseAmount(amount)}
    return nil
}

func (s *TaxContext) iCalculateTax() error {
    s.result = CalculateTax(s.invoice)
    return nil
}

func (s *TaxContext) theTaxAmountShouldBe(expected string) error {
    if s.result.Tax != parseAmount(expected) {
        return fmt.Errorf("expected %s, got %s", expected, s.result.Tax)
    }
    return nil
}

Implementation:

func CalculateTax(invoice *Invoice) *TaxResult {
    tax := invoice.Subtotal * 0.20
    total := invoice.Subtotal + tax
    return &TaxResult{Tax: tax, Total: total}
}

Key Concepts Covered

  • The TDD cycle: Red → Green → Refactor
  • Executable specifications with Godog
  • Step definition patterns and context management
  • Relationship between specifications and unit tests
  • Running acceptance tests vs. unit tests

Three Rules of Vibe Coding Applied

The tutorial demonstrates how this approach satisfies:

  • Easy to understand: Specification describes behavior clearly
  • Easy to change: Tests enable safe refactoring
  • Hard to break: Tests catch regressions immediately

Next Steps

After completing this tutorial, you'll understand the core TDD workflow. Continue to Building and Testing Changes to learn how to efficiently build and validate your changes.


Tutorials | How-to Guides | Explanation | Reference

You are here: Tutorials — learning-oriented guides that take you through steps to complete a project.