0% found this document useful (0 votes)
137 views4 pages

Summary Test Driven Development Work Flow

Document describe Test Driven Development work flow, this document elaborate how TDD for software development works and what is flow of TDD Software development methodology

Uploaded by

Babar Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
137 views4 pages

Summary Test Driven Development Work Flow

Document describe Test Driven Development work flow, this document elaborate how TDD for software development works and what is flow of TDD Software development methodology

Uploaded by

Babar Saeed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

The following sequence of steps is generally followed:

 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

 Create a simple String calculator with a method int Add(string numbers)


 The method can take 0, 1 or 2 numbers, and will return their sum (for an empty string it
will return 0) for example “” or “1” or “1,2”
 Allow the Add method to handle an unknown amount of numbers
 Allow the Add method to handle new lines between numbers (instead of commas).
 The following input is ok: “1\n2,3” (will equal 6)
 Support different delimiters
 To change a delimiter, the beginning of the string will contain a separate line that looks
like this: “//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where
the default delimiter is ‘;’ .
 The first line is optional. All existing scenarios should still be supported
 Calling Add with a negative number will throw an exception “negatives not allowed” –
and the negative that was passed. If there are multiple negatives, show all of them in the
exception message stop here if you are a beginner.
 Numbers bigger than 1000 should be ignored, so adding 2 + 1001 = 2
 Delimiters can be of any length with the following format: “//[delimiter]\n” for example:
“//[—]\n1—2—3” should return 6
 Allow multiple delimiters like this: “//[delim1][delim2]\n” for example “//[-][%]\n1-
2%3” should return 6.
 Make sure you can also handle multiple delimiters with length longer than one char

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.

Task: Create a String Calculator

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)

Test using Java for first requirement:

package com.wordpress.technologyconversations.tddtest;

import org.junit.Test;
import com.wordpress.technologyconversations.tdd.StringCalculator;

public class StringCalculatorTest {


@Test(expected = RuntimeException.class)
public final void whenMoreThan2NumbersAreUsedThenExceptionIsThrown() {
StringCalculator.add("1,2,3");
}
@Test
public final void when2NumbersAreUsedThenNoExceptionIsThrown() {
StringCalculator.add("1,2");
Assert.assertTrue(true);
}
@Test(expected = RuntimeException.class)
public final void whenNonNumberIsUsedThenExceptionIsThrown() {
StringCalculator.add("1,X");
}
}
Writing of Unit Source Code using Java

public class StringCalculator {


public static final void add(final String numbers) {
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) {
Integer.parseInt(number); // If it is not a number, parseInt will throw an exception
}
}
}
}

Requirement 2: For an empty string the method will return 0

Test using Java for second requirement:

@Test
public final void whenEmptyStringIsUsedThenReturnValueIs0() {
Assert.assertEquals(0, StringCalculator.add(""));
}

Writing of Unit Source Code using Java for second requirement

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

Analyze Requirement Specification,


Functionality and Internal Structure of the Unit
Code

Write Test Code using


Requirement/Functionality or Internal Structure
of Unit code (test Code will be executable
Program that will check functionality and
requirement)

Write Source Code for Unit

Run Written Test Code (Executable Code) over


Unit Source Code, If source code passes test it
Ok, Otherwise change it and Then Run test code
again, continue this process until source code
not full fill all specific requirements.

References:

 https://technologyconversations.com/2013/12/20/test-driven-development-tdd-example-
walkthrough/
 http://ariatemplates.github.io/Test-Driven-Development/aria_testcase.html

You might also like