Introduction to TDD

Welcome to the first lesson of our course on Test Driven Development (TDD) in Scala using ScalaTest. TDD is an iterative software development process where tests are written before developing the actual functionality. This approach helps developers focus on the requirements first, leading to more reliable and maintainable code.

In this lesson, we'll introduce you to the essential elements of TDD, including the Red-Green-Refactor cycle, which serves as the core structure of this methodology. We'll be utilizing tools specially suited for Scala: ScalaTest, a popular testing framework that integrates seamlessly with Scala, and Mockito, a mocking framework widely used in the Scala ecosystem, which we'll explore later in this path. Let's start by investigating TDD’s core components with a hands-on example.

Writing the First Test (Red)

The TDD process begins with writing a test that fails, marking the "Red" phase. This step allows you to crystallize what the code should achieve before writing the actual implementation. Let's write a test for a sum method that should eventually add two numbers.

Create a file named MathFunSuite.scala in the src/test/scala directory:

import org.scalatest.funsuite.AnyFunSuite

class MathFunSuite extends AnyFunSuite:

  test("sum adds two numbers correctly"):
    assert(sum(2, 3) == 5)

This test script:

  • Uses test to denote a single test case.
  • Calls the sum method and checks if the result equals 5.
  • assert(condition) verifies that the actual result matches the expected value.

This immediate failure highlights the importance of the "Red" phase in TDD, as it confirms that our test is effectively identifying missing features and setting clear implementation goals.

Expected output:

[error] -- [E006] Not Found Error: /src/test/scala/MathFunSuite.scala:6:11 
[error] 6 |    assert(sum(2, 3) == 5)
[error]   |           ^^^
[error]   |           Not found: sum

This is a normal failure, illustrating that our test is effectively identifying unimplemented features.

Making the Test Pass (Green)

Our next objective is to write the simplest code possible to make the test pass — the "Green" step. In TDD, this means implementing minimal functionality to satisfy the test conditions. Let’s define the sum method in a new file Math.scala under the src/main/scala directory:

def sum(a: Int, b: Int): Int = 5

This version seems superficial since it doesn't actually add two numbers, but it highlights the TDD focus on passing the test with minimal implementation. By doing so, we've met the test condition.

Re-running the test will produce the following result:

[info] MathFunSuite:
[info] - sum adds two numbers correctly
[info] Run completed in 266 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.

Seeing the test pass confirms our code meets the given test scenario. Future test cases will guide us to refine the implementation.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal