Summary Test Driven Development Work Flow
Summary Test Driven Development Work Flow
Test-driven development (TDD) is a software development process that relies on the repetition
of a very short development cycle: first the developer writes an (initially failing) automated test
case that defines a desired improvement or new function, then produces the minimum amount of
code to pass that test, and finally re-factor the new code to acceptable standards.
Add a test
Run all tests and see if the new one fails
Write some code
Run tests
Re-factor code
Repeat
As a initial step of TDD, Developer must have complete and precise information regarding the
unit code he is going to write. Developer must have concise information regarding functions,
algorithms and internal structure of the unit code and as well all the requirement specification
about the developed unit code. On the basis of information about under development unit code,
developer will made list of requirements of developed unit code. After the analysis of
requirement, on the basis of requirement and functionality of the unit code developers write test
code those are executable programs to testify the functionality and requirement of the developed
code.
Below if the example of test code related to each requirement and afterwards the actual
implementation. Take only one requirement, write the tests and then implement it as unit code.
Requirements Analysis
First Step of TDD, Analysis the requirements, functionality and internal structure of the unit that
have to develop. Elaborate each requirement and write test for each requirement individually.
After writing test for each requirement actual source code for the unit will be write and examine
through the running of test cases. Example is as following
Even though this is a very simple program, just looking at those requirements can be
overwhelming. Go through each requirement one by one and write test code for each
requirement and then its actual source code.
Requirement No. 1: The method can take 0, 1 or 2 numbers separated by comma (,).
First requirement for creation of a string calculator is that, the method used in this calculator can take only
two strings, null value. (Null Value, 1 String or 2 String)
package com.wordpress.technologyconversations.tddtest;
import org.junit.Test;
import com.wordpress.technologyconversations.tdd.StringCalculator;
@Test
public final void whenEmptyStringIsUsedThenReturnValueIs0() {
Assert.assertEquals(0, StringCalculator.add(""));
}
public static final int add(final String numbers) { // Changed void to int
String[] numbersArray = numbers.split(",");
if (numbersArray.length > 2) {
throw new RuntimeException("Up to 2 numbers separated by comma (,) are allowed");
} else {
for (String number : numbersArray) {
if (!number.isEmpty()) {
Integer.parseInt(number);
}
}
}
return 0; // Added return
}
Test Driven Development Work Flow
References:
https://technologyconversations.com/2013/12/20/test-driven-development-tdd-example-
walkthrough/
http://ariatemplates.github.io/Test-Driven-Development/aria_testcase.html