Skip to content

Latest commit

 

History

History
64 lines (48 loc) · 2.29 KB

tdd-rust-guidelines.md

File metadata and controls

64 lines (48 loc) · 2.29 KB

Test-Driven Development (TDD) Basics in Rust

Core Concepts

Test-Driven Development (TDD) follows this development cycle:

  1. Red: Write a failing test first.
  2. Green: Implement the minimum necessary code to pass the test.
  3. Refactor: Improve the code while ensuring tests still pass.

Key Principles

  • Tests define the specification: Test code expresses the expected behavior of the implementation.
  • Follow the Arrange-Act-Assert pattern:
    1. Arrange: Set up the necessary test environment.
    2. Act: Execute the functionality under test.
    3. Assert: Verify the expected result.
  • Test names should follow a "Condition → Action → Expected Result" format. Example:
    • "Given a valid token, retrieving user information should succeed"

Essential Tools for the Refactoring Phase

Once tests pass, use the following tools to refine your code:

1. Static Analysis & Linting

  • Run cargo check for type checking and borrow checking.
  • Use cargo clippy to detect potential issues and enforce best practices.

2. Dead Code Detection & Removal

  • Run cargo deadlinks to check for dead documentation links.
  • Use cargo udeps to find unused dependencies.
  • Run cargo rustc -- -W dead_code to detect unused functions.

3. Code Coverage Analysis

  • Install cargo-tarpaulin for test coverage measurement:
    cargo install cargo-tarpaulin
    cargo tarpaulin --out html
  • Open the generated HTML report to review coverage.

4. Version Control with Git

  • Commit after each phase (test creation → implementation → refactoring).
  • Review changes before committing:
    git status  # Check modified files
    git add <relevant files>
    git commit -m "<appropriate commit message>"
  • Use commit prefixes for clarity:
    • test: - Adding or modifying tests
    • feat: - Implementing new features
    • refactor: - Code refactoring

Further Reading

For more details on TDD practices in Rust, naming conventions for tests, and best practices for refactoring, refer to:

.docs/tdd-rust-guidelines.md

This file includes step-by-step instructions for test-first development, structuring test cases, and leveraging Rust’s testing framework efficiently.