Prefer data-driven automated tests

When designing an automated test at any level, it is worth considering if you can make that test more data-driven.

Rather than hard-coding the input values and expected results into the test logic, separate them out.

The Spock framework has some elegant support built in for data-driven tests,  …

… but you can also do similar in good old JUnit and TestNG


As explained in the Spock docs:

  • Code and data are not mixed together and can be changed independently more easily
  • Data for the tests can be generated using some other tool or fetched from external sources
  • it is easy to increase useful test coverage by adding different input data and expected output data to the same scenario

The built-in support in the automated test framework means that:

  • In order to exercise the same code multiple times, it does not have to be duplicated or extracted into a separate method
  • In case of a failure, it is immediately clear which inputs caused the failure
  • Exercising the same code multiple times benefits from the same isolation as executing separate methods does