ThisCalculator

Everyday

Test Driven Development: How Small Tests Clarify Next Steps

Updated · Standard formula, shown below

Average time per test 3.00 min
Tests per hour
20.00
Result
Total time for all tests
30 min
You entered
Number of tests written
10
You entered

Test-driven development (TDD) makes the next step clear by forcing you to write a failing test for one small behavior before writing any production code, so you always know exactly what to implement next: make that test pass. This article explains how that cycle works, why it reduces decision paralysis, and how to apply it effectively.

Formula

The calculator above estimates the average time you spend per test and the number of tests you can write per hour. The formula for average time per test is:

Average time per test = Total time / Number of tests

Where:

  • Total time (minutes) – the total time spent writing and running tests.
  • Number of tests – the count of tests written in that period.

The tests-per-hour figure is simply the number of tests divided by the total time in hours (total minutes / 60).

Worked Example

Using the default values: you spend 30 minutes writing 10 tests.

  1. Average time per test = 30 / 10 = 3 minutes per test.
  2. Tests per hour = 10 / (30/60) = 10 / 0.5 = 20 tests per hour.

The calculator shows 3.00 minutes per test and 20 tests per hour.

What Is Test-Driven Development?

Test-driven development is a software development practice where you write a failing test before writing the code that makes it pass. The cycle is often described as red-green-refactor:

  • Red – write a test for a small piece of behavior. Run it and see it fail (because the behavior doesn’t exist yet).
  • Green – write the simplest code that makes the test pass.
  • Refactor – clean up the code while keeping the test green.

This cycle is repeated many times per feature, each time adding one small behavior. The key is that each test is small and specific, so the next step is always obvious: make that one test pass.

Why Small Tests Make the Next Step Clear

When you write a failing test, you define the expected behavior in an executable form. The test acts as a specification for the next piece of code. Because the test is small, it covers only one behavior, so you don’t have to decide between multiple possible implementations. You just write the simplest code that satisfies that test.

For example, if you’re building a calculator, you might write a test that asserts add(2, 3) == 5. The next step is clear: implement an add function that returns the sum of two numbers. You don’t need to design the whole calculator at once; you just make that one test pass.

This approach reduces the cognitive load of large design decisions. Instead of trying to plan every detail upfront, you let the tests guide you one small step at a time. Each test gives you a concrete goal, and once it passes, you have a working piece of functionality that you can build upon.

How to Choose What to Test Next

Choosing the next test is often the hardest part for beginners. A common strategy is to pick the simplest behavior that is still missing from your program. You can list the behaviors you want and order them from simplest to most complex. Then, write a test for the simplest one that isn’t implemented yet.

Another approach is to write tests that cover edge cases or error conditions after the happy path works. For example, after testing add(2, 3), you might test add(-1, 1) or add(0, 0). Each test adds a small constraint, and the implementation evolves to handle it.

Some developers use a technique called “test-first” where they write tests for the public API of a class before writing the class. This forces you to think about the interface first, which clarifies what the next step should be: implement that interface.

Common Mistakes and Misconceptions

One common mistake is writing tests that are too large or too vague. A test that checks multiple behaviors at once gives you less guidance because you have to implement several things to make it pass. Keep tests focused on one behavior.

Another mistake is skipping the refactoring step. If you only write tests and code, you may end up with messy code that is hard to change. The refactor step is essential to keep the code clean and maintainable.

A common misconception is that TDD slows you down. While it may take longer initially, it often saves time later by reducing bugs and making changes easier. However, TDD is not a silver bullet; it works best for well-defined logic and less well for UI or complex integrations.

Another misconception is that you must test everything. In practice, you should focus on the critical business logic and edge cases. Testing trivial getters and setters is usually not worth the effort.

How to Read the Result of Your TDD Practice

The calculator above gives you a rough idea of your testing speed. If you find you’re spending more than 5 minutes per test, you might be writing tests that are too large or over-thinking. If you’re writing many tests per hour but still seeing bugs, you might be testing the wrong things or not covering edge cases.

More importantly, the “result” of TDD is the quality of your code and the confidence you have in it. A good sign is that you can make changes to your code and the tests catch regressions. Another sign is that you rarely need to debug because the tests pinpoint the exact failure.

Limits of TDD

TDD is not suitable for every situation. It is difficult to apply to exploratory programming where the requirements are unclear, or to code that depends heavily on external systems like databases or network calls (though you can use mocks). It also requires discipline and practice; it’s easy to fall back to writing code first.

Another limit is that TDD does not guarantee correct requirements. If your tests are based on a wrong understanding of the problem, you’ll build the wrong thing. TDD is about building the software right, not necessarily building the right software.

Frequently Asked Questions

Does TDD mean I have to write tests before any code?

Yes, in strict TDD you write a failing test before writing the production code. However, some developers use a looser approach where they write tests after the code but still use the red-green-refactor cycle. The key is that the test drives the design.

What if I can’t think of a test to write?

Start with the simplest behavior you can think of, even if it’s trivial. For example, if you’re building a function that returns a string, test that it returns an empty string initially. This gets you into the cycle, and the next test often becomes obvious once you see the code.

How do I test private methods?

You generally shouldn’t test private methods directly. Instead, test the public behavior that uses them. If you find you need to test a private method, consider extracting it into a separate class or making it public if it’s a meaningful unit of behavior.

Is TDD only for unit tests?

No, TDD can be applied at different levels, including integration tests and acceptance tests. However, the classic TDD cycle is usually applied at the unit level because it gives the fastest feedback and the smallest steps.

What if my tests take too long to run?

If your tests are slow, you may have too many integration tests or tests that hit external services. Keep your unit tests fast by mocking dependencies. Run the fast tests frequently and the slow tests less often.

Can TDD be used with legacy code?

Yes, but it’s harder. You often need to write characterization tests that capture the current behavior before you change anything. Then you can use TDD for new features or bug fixes.

Related

Formula used
in t | Total time for all tests | min | 30 | 0
in n | Number of tests written | - | 10 | 1
out avg | Average time per test | min | t / n | 2
out perhour | Tests per hour | - | n / (t / 60) | 2

Each line is one input (in, sel, list) or one result (out: label, unit, formula, decimals). How the calculators work

Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.