GoalFollow the loop of iterative development, and use a failed test to correct a micro:bit program one version at a time.
New words
iterative development · making a program one part at a time: build, test, decide what to do next, and repeatversion · one stage of a program as it grows, such as version 1 and version 2boundary test · a test with the value exactly at the edge of a rule, such as the 10th jump when the rule says 10 or more
Explanation
In iterative development you do not write the whole program at once. You write the requirements and choose the first part. Then you build it and test it. After the test you decide what to do: change the part if the idea was wrong, correct it if it does not work, or add the next part. After every change you test again, and you go on round the loop until all the requirements are met. Each pass gives a new version of the program. Tests should include the value at the edge of a rule, because a wrong comparison such as > instead of >= shows up there.
Worked examples
Version 2 adds requirement 2. Button B sets jumps to 0 and shows it. The test is: shake three times, press B, shake once. The lights show 1, 2, 3, then 0, then 1. The test passes, so the next part can be added.
Test
Shakes
Expected lights
Actual lights
Result
1
9
9
9
pass
2
10
happy face
10
FAIL
3
11
happy face
happy face
pass
Version 3, first try. The programmer wrote jumps > 10 for the happy face, but the requirement says 10 jumps or more. The test plan below covers the edge of the rule. Test 2 fails: at 10 jumps the lights show the number 10, and the happy face should have appeared. The failing row is highlighted.
Version 3, corrected. The test showed the fault, so the comparison changed to jumps > 9. Now the 9th shake shows 9, the 10th shows the happy face, and the 11th shows the happy face again, because jumps is 11. Pressing B shows 0, and the next shake shows 1. The loop would now go round again for the next requirement.
Class activity
Build in layers (no computer). Cut paper strips and write on each strip one micro:bit block from the jump counter, such as set jumps to 0 or change jumps by 1. Build version 1 from the strips. A partner is the micro:bit: they say what the lights show after the events you call out, such as ‘shake, shake’. Then add the strips for version 2 and call out a new list of events that includes B. (If you have a micro:bit, the block editor and an adult nearby, you can build version 1 and shake it.)
Practice
1
The user presses A four times. What do the lights show after each press?
After the first press 3, after the second 6, after the third the yes icon, and after the fourth the yes icon. points is 3, 6, 9 and 12. The condition points > 8 is false for 3 and 6, and true for 9 and 12.
2
The user shakes four times and then presses B. What do the lights show after the last press, and does the program meet the requirement ‘B starts again from 0’? How would you repair it?
The lights still show 4. The variable jumps does change to 0, but nothing shows it until the next shake, so the user sees the old number. The requirement is not met as the user sees it. Add show number (jumps) after set jumps to 0 in the B block.
3
Put these steps of iterative development in order: test the finished program; build the first part; decide to change, correct or add; write the requirements; test the part; choose which part to make first.
1 write the requirements. 2 choose which part to make first. 3 build the first part. 4 test the part. 5 decide to change, correct or add, and go back to build and test again. 6 test the finished program.
4
A rule says that the happy face must appear at 6 jumps or more. Write the condition for the IF block. Then write three tests, with the number of shakes and the lights you expect.
The condition is jumps > 5. Tests: 5 shakes should show 5, 6 shakes should show the happy face, 7 shakes should show the happy face. The test at 6 is the one that shows an error such as jumps > 6.