Week 25 · Unit 10: Be a game developer · 3 lessons
3
Test, fix and improve
Computing 5: Unit 10, pp. 129–144
GoalTest a game against its criteria, fix a bug that a test finds and add a new criterion.
New words
test plan · a list of things to do and the result that should happen for each onefix · to change the code so that a bug is goneimprove · to make a program better by adding a new criterionretest · to run the tests again after a change
Explanation
A test plan has one test for each criterion. We do the test and write the result. When a test fails, we look at the code that belongs to that criterion and find the bug. Then we fix it. After any change, we retest all the criteria, because a fix can break something that worked before. When every test passes, the program meets its criteria. Then we can improve it by adding a new criterion, and we write the tests for it first. A program is never finished until every criterion has a yes.
Criterion
Test
Result
5 A missed apple starts again at the top
let an apple fall past the basket
?
Worked examples
Criterion
Result
Notes
1 Basket moves
yes
2 Apple falls
yes
3 Score goes up by 1
yes
4 New apple after a catch
yes
5 New apple after a miss
no
apple stuck at the bottom
6 ‘You win!’ at 5
yes
We run ‘Catch the apple’ and test each criterion. The apple code has no block for a missed apple, so criterion 5 fails. A missed apple stays stuck at the bottom edge of the stage, so no new apple falls and the game cannot reach a score of 5.
The fix is a second if block inside the loop. When the apple’s y position is below −170, it goes back to the top at a random place. After the fix we retest all six criteria.
Now we improve the game. New criterion 7: ‘The game says Game over when 3 apples have been missed.’ We need a new variable, misses. It starts at 0 and goes up by 1 for every miss.
Class activity
Test lab (3 players, paper). Draw a stage on paper and cut out a basket and an apple. Player 1 moves the apple down one square for every tap on the table. Player 2 moves the basket one square left or right when asked. Player 3 is the tester and holds the six criteria. Play until the apple reaches the bottom row. The tester ticks yes or no. Then change the rules so that a missed apple goes back to the top row, and play again. Which criterion changed from no to yes?
Practice
1
Look at this boat game test. Which criteria fail? Where should you look in the code first?
Criterion
Result
1 The boat moves right with the right arrow
yes
2 The boat moves left with the left arrow
yes
3 Each fish caught gives 1 point
no
4 The game says ‘Well done!’ at 10 points
no
Criteria 3 and 4 fail. Look first at the ‘if touching Fish’ block and the ‘change score by 1’ block inside it. Criterion 4 may fail only because the score never goes up, so fix criterion 3 and then retest criterion 4.
2
A tester fixed criterion 5 of a game and then ran all the tests again. Why was the retest worth doing? What does the table show?
Criterion
Before the fix
After the fix
1
yes
yes
2
yes
yes
3
yes
no
4
yes
yes
5
no
yes
A fix can break something that worked before. The table shows that criterion 3 passed before the fix but fails now, so the tester must look at the code again.
3
Write a test for the criterion ‘The player starts with 3 lives’.
Click the green flag and read the variable lives. It should show 3.
4
A new criterion says: ‘The game lasts 60 seconds, then it says Time up and stops.’ Which Scratch blocks could you use, and how would you test it?
Use reset timer after the green flag, then wait until <(timer) > (60)>, then say Time up and stop all. Test: start the game, do not catch five apples, wait one minute and check that it says Time up and stops.