Activity: Get started with TDD

(no submission)
Purpose:

Require: Java 8 (or higher), JUnit (you are recommended to use the latest version, JUnit 5).
Additional resources: You may find class discussions on RIPR model, test automation, and characteristics of good tests helpful when designing and automating your tests.


Instruction:

You may work on this activity alone or with as many partners as you want

For this activity, we will stop periodically and do group work one step at a time with discussion between steps.

You are encouraged to work through the TDD process in your favorite IDE. If you run into anything you are not sure about, post to Piazza, or ask in class.

There are two goals here. First, learning TDD is best accomplished by doing TDD. Second, applying TDD to develop any software applications follows the same process, except additional factors need to be taken into account when developing large-scale or complex software.


Imagine we are playing a "Duh-Huh" game. I will ask for volunteers (or randomly select names from the roster). Each volunteer will take turns saying the next number in sequence (starting from one), "Duh," "Huh," or "DuhHuh."

If the number is divisible by three, you will say "Duh." If it is divisible by four, you will say "Huh." If the number is divisible by three and four, you will say "DuhHuh."

(This activity would be more fun if you put your creativity + heart and soul into saying it!!)

Let's try!!


To make the "Duh-Huh" game simple, let's write a program to pre-print what to say so that the players can simply read the pre-print strings.


Now, follow the TDD process, develop a program to pre-print what to say (assume the number from 1 to 100).

Sample output:

1 2 Duh Huh 5 Duh 7 Huh Duh 10 11 DuhHuh 13 14 Duh Huh 17 Duh ... (up to 100)

Sample user story:

Imagine we are implementing a program to pre-print strings to help players play a "Duh-Huh" game. To play a "Duh-Huh" game, players will take turns saying the next number in sequence (starting from one), "Duh," "Huh," or "DuhHuh." If the number is divisible by three, the player will say "Duh." If the number is divisible by four, the player will say "Huh." If the number is divisible by three and four, the player will say "DuhHuh." That is, to pre-print what to say from 1 to 100, the program prints "1, 2, Duh, Huh, 5, Duh, ..., 95, DuhHuh, 97, 98, Duh, Huh"


Alternative user stories:

User story #1: A player will say the number in sequence (starting from one).

User story #2: A player will say "Duh" if the number is divisible by three.

User story #3: A player will say "Huh" if the number is divisible by four.

User story #4: A player will say "DuhHuh" if the number is divisible by three and four.

User story #5: The program prints "1, 2, Duh, Huh, 5, Duh, ..., 95, DuhHuh, 97, 98, Duh, Huh" to say 1 to 100.

User story #6 (optional): Players will take turns saying the next number in sequence (starting from one), "Duh," "Huh," or "DuhHuh." — [design decision] You need to decide whether this requirement must be implemented.


Step 1: Transform the user story into requirements, and then requirements into tests.

In this step, you will transform the user story into requirements. That is, you will decompose the user story into a list of "requirements as tests" — things to verify (not "requirements as tasks" — things to do)

Example requirement as test: (what should be done) 
    The string "Duh" is printed when the current number to say is divisible by three.  
Example requirement as task: (what we should do)
    Implement a program that prints the string "Duh" when the current number to say is divisible by three.
Example test description: 
    Evaluating a program to pre-print a string up to number 3 results in "1 2 Duh"
    (That is, test input = 3, expected output = "1 2 Duh")
Key idea:

Step 2: Write a test (code)
Key idea:

Step 3: Run the test

Step 4: Write (Java) code to make the test pass
Key idea:

Step 5: Repeat steps 3 and 4 until the test passes

Step 6: Refactor
Key idea:

Step 7: Repeat steps 2-6 until all tests associated with the chosen requirement have been covered.

Step 8: Repeat steps 1-7 until all requirements have been fulfilled.

Note: to help you review the TDD concept, you should use the TDD slides for reference.




Copyright © 2025 Upsorn Praphamontripong
Released under the Creative Commons License CC-BY-NC-SA 4.0 license.
Last updated 2025-11-14 19:48