How Test Driven Development Helps You Get Things Right

On the first try. No rewrites.

2 min read

Writing test cases first helps us understand better what we need to build.

Tests allow us to express how the code should behave.

If we focus on asserting the behavior of code, we end up with tests that are also a functional documentation.

A business expert or other developer can read the tests and quickly understand what the code does, making it easier to correct any misunderstandings.

❌ Change your tests like these:

describe("validate", {
  it("should work correctly", {
    # Complex setup
    # Many expectations
  })
})

✅ To tests like these:

describe("validate", {
  it("should return TRUE if data passes validation", {
    # Arrange

    # Act

    # Assert

  })

  it("should return FALSE if data does not pass validation", {
    # ...
  })

  it("should throw an error if input is not a table", {
    # ...
  })
})

Before writing each test case, ask yourself:

Answering those questions:

And it all comes down to writing tests first. Build the right thing on the first try.