Doubles on Stage
v1.0
CC BY-SA 3.0
Olivier Azeau
http://agilitateur.azeau.com/
Prologue
CODER is reading the xUnit Test Patterns book.
NARRATOR
Coder is upset. She has always seen herself has a software development virtuoso. But when her team mates told her that, from now on, they would use TDD, she had no other option. Tests, Red, Green, Refactor... She had to learn all those new words and she had to do it right now. She starts reading Gerard Meszaros' xUnit Test Patterns but quickly falls asleep.
Scene 1 - a first test
In a quiet, idle, execution environment, TEST is pacing up and down.
CODER walks in slowly in this unknown place. PROGRAM, SENSOR and TIMER are following her.
PROGRAM
Are you sure this is the place?
CODER
We'll see. Don't be afraid. Let's move on. I've been told that testing environment are safe places.
TEST
Hi there! Are you here for TDD?
CODER
Indeed... How did you know?
TEST
For which other reason would you come here?
Where is your system under test?
CODER
What is it?
TEST
A program to design using TDD. In our jargon, we call it a system under test.
CODER
(turning back to PROGRAM and pointing a finger at it)
It is there!
PROGRAM steps backward.
TEST
Come here! Don't be afraid!
(showing the center of the stage)
Stand there so that everybody can see you.
PROGRAM obeys and go to the center of the stage.
CODER
We were not introduced to each other, did we?
TEST
You can call me TEST. At your service.
What is the goal of your program?
CODER
It shall tell, in the long run, the maximum value of the ambient temperature.
TEST
And do you know the current temperature?
CODER
No, but I can find it out. I brought a temperature sensor.
(turning to SENSOR)
What is the current temperature?
SENSOR
22 degrees
CODER
(turning to TEST)
22 degrees!
TEST
Good! Let's start here.
(talking to PROGRAM)
What is the maximum temperature?
Surprised by the question, PROGRAM does not know what to say.
Acting like a football referee, TEST shows a red card.
CODER
Hey! Be nice to my program! It hasn't been programmed yet!
TEST
Exactly. That's what I call a good start. With a red test, we define a goal. This is not a punishment but an opportunity for improvement. Now we will program it as simply as possible to get "22 degrees" as an answer.
CODER
(talking to PROGRAM)
When someone asks you "What is the maximum temperature?", you just answer "22 degrees".
TEST
What is the maximum temperature?
PROGRAM
22 degrees
TEST shows a green card to PROGRAM
CODER
Look where that's got us: a parrot program!
Scene 2 - with a sensor
TEST
It is just a beginning. Now, let's try to improve the test to clarify the intention.
Usually, a test has three phases: Arrange-Act-Assert.
"Act" is when I ask the question to execute the code being tested.
"Assert" is when I check the result is as expected.
CODER
And we miss the "Arrange" phase. The one where I would have set up my sensor, assuming I had the time to do so...
TEST
That's the idea. But let's think about testing some temperature that do not match with the sensor figures. For exemple, imagine that the temperature is 18 degrees. How can we test that?
CODER
We will have someone to adjust the air conditioning?
TEST
We can do something simpler. I know someone who can help us.
(raising the voice)
STUB, where are you? Come here!
STUB enters the room
STUB
Hello, I'm STUB. I provide indirect inputs to a system under test.
TEST
(talking to STUB)
When someone asks you "What is the current temperature?", you shall say "18 degrees".
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
22 degrees
TEST shows a red card to PROGRAM
CODER
Here we go again. I did not even had the time to program it!
TEST
If you had not seen the red card, you would not even know there was something to program!
CODER
(talking to PROGRAM and showing STUB)
You see this sensor? When someone asks you "What is the maximum temperature?", You ask the sensor "What is the current temperature?". The sensor will give you an answer and you will simply give this answer to the one who has asked you the question.
TEST
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to TEST)
18 degrees
TEST shows a green card to PROGRAM
Scene 3 - with a screen
CODER
I must admit that the practice sparks my interest. And if, instead of giving the answer directly, my program would write it on a screen, it would be a bit more complicated to check, right?
TEST
No. Actually it is very simple. I know a spy who can help us on this one.
(raising his voice)
SPY, come and help us, please?
SPY enters the room
SPY
Hello guys. I'm SPY. I record the indirect outputs of a system under test so that anyone can find out what happened.
TEST
Stop showing off. When someone tells you to display a value, you memorize it because I will need this value and I will ask you later.
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to TEST)
18 degrees
TEST
(talking to SPY)
What is the last displayed value?
SPY
Nothing was displayed.
TEST shows a red card to PROGRAM
CODER
It's ok I got it. I will change my program.
(talking to PROGRAM and showing SPY)
When the sensor gives you an answer, you ask this screen to display it and you announce that you are done.
TEST
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display "18 degrees".
I'm done!
TEST
(talking to SPY)
What is the last displayed value?
SPY
18 degrees.
TEST shows a green card to PROGRAM
Scene 4 - A flow of values
CODER
Now I would like to refine the logic of my program. If I want this sensor surrogate, this stub, to return multiple values, shall I just tell it to do so?
For example, if 18 degrees is measured in the first place and then the second measurement is 25 degrees, the maximum should be 25 degrees.
TEST
The simplest way is often to try. We have nothing to lose.
(talking to STUB)
When someone asks you "What is the current temperature?", the first time, you answer "18 degrees". And when asked a second time, you answer "25 degrees".
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
I'm done!
TEST
(talking to SPY)
What is the last displayed value?
SPY
18 degrees.
TEST shows a red card to PROGRAM
CODER
Now I can code the comparison logic to compute the maximum. I got it!
TEST
Not so fast! Keep in mind that one should make only minimal changes to the code: just enough code to pass the test.
CODER
Oh... Let me think about it...
(talking to PROGRAM)
When someone asks you the maximum temperature, you will repeat _twice_ the sequence you are used to do, asking the sensor and displaying the value, before notifying that you are done.
TEST
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
(talking to STUB)
What is the current temperature?
STUB
25 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
I'm done!
TEST
(talking to SPY)
What is the last displayed value?
SPY
25 degrees.
TEST shows a green card to PROGRAM
Scene 5 - with a timer
CODER
It's weird. The test is green but I'm not happy with the current code. Why repeating the sequence twice? Why not three times? Or even four times?
Do I have to test it with all integers? It could last for a while ...
TEST
Imagine for a moment that the program leaves this test environment to go to its real environment, how would this work?
CODER
In the real world, I've got this timer.
CODER shows TIMER who remained on the sideline since the beginning
TEST
What is it used for?
CODER
It gives the pace. Actually, the program never ends but it asks the timer to notify at regular intervals so that the measurement of the temperature and all that follows can happen.
TEST
You can try to change the program by incorporating the timer...
CODER
Do I have the right to do that? I thought that I needed a red test before making changes.
TEST
There are two reasons driving the program changes. The first one is when one wants to extend the program functionality. You need a red test for that. This is what we have done so far.
The other reason is when the lights are green but the current design is not satisfactory. We can then refactor the program without adding any new feature.
CODER
Good! I'm going to add my timer.
(talking to TIMER)
Go over there, behind the program.
TIMER goes behind PROGRAM
CODER
(talking to PROGRAM)
When someone asks you the maximum temperature, you will, as usual, ask the sensor and display the value. But then, you will ask this timer, behind you, to warn you in ten seconds.
Then, you wait.
When the timer notifies that the ten seconds are over, you repeat again the whole sequence just as if someone had asked you the maximum temperature.
TEST
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
(talking to TIMER)
Wait for ten seconds!
TIMER counts slowly and silently up to ten. The room is quiet.
TIMER
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
25 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
(talking to TIMER)
Wait for ten seconds!
TIMER starts again to count in the quiet room.
After five seconds, CODER cannot wait any longer and burst out.
CODER
Stop! That will never work!
Scene 6 - with a fake timer
CODER
It cannot work! My timer will never stop and we will not be able to test anything.
Moreover, we spend most of our time waiting for the timer! Tests would run for ages.
TEST
I'm glad you noticed it. There are developers who persist and continue to use real third party components during testing.
CODER
But if we cannot use a real timer, what are we going to use?
TEST
Easy-peasy: we will use a fake one!
There is one waiting for us.
(talking to FAKE)
FAKE, come here. Could you please replace that timer?
FAKE enters the room and pushes TIMER to take his place.
FAKE
Move off. The double are in the place now.
TEST
(talking to FAKE)
FAKE, today, you are a timer.
When someone asks you to wait for a number of seconds, you immediately reply that the time is up.
And if you are asked again, you do not answer but you tell me that it's over.
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
25 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
(talking to TEST)
It's over!
TEST
(talking to SPY)
What is the last displayed value?
SPY
25 degrees.
TEST shows a green card to PROGRAM
Scène 7 - final program
CODER
It's nice. My program begins to have a good shape and the test runs more quickly than I could have imagined.
Let's try to complete this stuff.
(talking to TEST)
It's the final round. We will have three input values: 18 degrees, 25 degrees and 10 degrees. We will have our fake timer to wait twice instead of once and, in the end, we should have a display showing 25 degrees.
TEST
(talking to STUB)
When someone asks you "What is the current temperature?", the first time, you answer "18 degrees", the second time, you answer "25 degrees", and the third time, you answer "10 degrees".
(talking to FAKE)
When someone asks you to wait for a number of seconds, you immediately reply that the time is up.
If you are asked a second time, you reply that the time is up too.
And if you are asked a third time, you tell me that it's over.
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
25 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
10 degrees
PROGRAM
(talking to SPY)
Display 10 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
(talking to TEST)
It's over!
TEST
(talking to SPY)
What is the last displayed value?
SPY
10 degrees.
TEST shows a red card to PROGRAM
CODER
Let's go!
(talking to PROGRAM)
You will have to do something else when the sensor gives you an answer.
If this is the first time you just memorize the value. Upon each other answer, you compare it with the value that you memorized and you keep the largest of the two values in your memory.
In both cases, you display the memorized value.
TEST
(talking to PROGRAM)
What is the maximum temperature?
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
18 degrees
PROGRAM
(talking to SPY)
Display 18 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
25 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
Time is up!
PROGRAM
(talking to STUB)
What is the current temperature?
STUB
10 degrees
PROGRAM
(talking to SPY)
Display 25 degrees.
(talking to FAKE)
Wait for ten seconds!
FAKE
(talking to TEST)
It's over!
TEST
(talking to SPY)
What is the last displayed value?
SPY
25 degrees.
TEST shows a green card to PROGRAM
CODER
That's all good. I think we can go to bed.
EVERYBODY
CODER, Wake up!