Code Editor
Terminal
Learn
Learn TDD With Mocha

Introduction

Test-driven development (TDD) is a programming technique where you write test code before implementation code. The tests are written to define the desired behavior of your program. The test output provides descriptive error messages that inform the implementation of your program.

One strategy for writing tests before implementation code is the red-green-refactor cycle:

  • Red — Write tests that describe the intended behavior of implementation code. The tests will fail, which is expected since we have not yet written the implementation code. “Red” and “green” correspond to the color of the logs that our test framework produces while running the tests in our suite. Red signifies failing tests and green corresponds to passing tests.
  • Green — Write implementation code that makes your tests pass. The tests return green because the implementation code executes the intended behavior described by the tests.
  • Refactor — Adjust tests to reflect characteristics of a good test. Refactor implementation code to be more descriptive and efficient. The tests are passing, and should continue to pass throughout this phase of the cycle.

In this lesson, you will use a test-driven development approach to program a method that calculates the sum of an array of numbers. You will also build an effective suite of tests for your implementation code.

Report a Bug
If you see a bug or any other issue with this page, please report it here.