Check the brakes first
A bike builder checks the brakes, the gears and the chain one by one. Only then does she put the bike together.
Imagine she tested only the finished bike. A bad brake would be hard to find. Software is built the same way. You test the small parts first, then bigger and bigger parts.
Key words
| Word | What it means | Example |
|---|---|---|
| Component | One part of a solution. | a function, a device or a screen |
| Interface | The place where two parts meet and pass data. | a screen and a database |
| Unit test | A test of one small part on its own. | |
| Integration test | A test that checks joined parts work together. | |
| System test | A test of the whole system against its requirements. | |
| Acceptance test | The customer tests the product and decides to accept it. | |
| Boundary test | A test with values on the edges of what is allowed. | |
| Concept test | A test of an idea or a design before it is built. | |
| Requirement | Something the solution must do. | |
| Function | A named piece of code that does a job. | |
| Prototype | A simple early model of a product, used to try out an idea. | |
| Database | An organised store of data that programs can read and change. |
The cells students fill in are left blank in this preview.
Five things you test
| Part | What it is | An example test |
|---|---|---|
| Software | The code, such as one function | Does the fare function give 2.50 for an adult? |
| Hardware | The devices and parts | Does the card reader read a card? |
| Data | Files and databases | Is every price in the file a number? |
| Interfaces | Where two parts meet | Does the screen send the right data to the database? |
| Resulting service | The finished product | Can a customer buy a ticket from start to end? |
The cells students fill in are left blank in this preview.
Quick check: which part?
Have a go
Quick check: which part?
4 questionsWhy test the parts first?
A bug in a small part is easy to find. You know which part to look at.
A bug in a finished product could be anywhere. Fixing a bug early is cheaper than fixing it late. And when every part works, you can trust them when they are joined.
Testing grows from small to big
Six testing methods
| Method | Purpose | When it is used |
|---|---|---|
| Concept testing | Find out if an idea or design is worth building | At the start, with a sketch or a prototype |
| Unit testing | Check one small part on its own | Every time a part is written or changed |
| Boundary testing | Check the edges of the allowed values | When a part has a limit, such as an age range |
| Integration testing | Check that joined parts work together | When parts are put together |
| System testing | Check the whole system against the requirements | When all parts are joined |
| Acceptance testing | The customer checks it does what they need | At the end, before they accept the product |
The cells students fill in are left blank in this preview.
Try it: a unit test
def fare(age):
if age < 16:
return 1.0
return 2.5
# A unit test checks one small part on its own.
assert fare(10) == 1.0
assert fare(40) == 2.5
print("Both unit tests passed")
# Add a third test: assert fare(16) == 2.5
# Then break the function and see the test fail.
Make a free account to save your work and track your progress.
Try it: boundary tests
def is_teenager(age):
return age >= 13 and age <= 19
# Test the edges: one below, on the edge, on the edge, one above.
for age in [12, 13, 19, 20]:
print(age, is_teenager(age))
# Change >= 13 to > 13. Which age now gives the wrong answer?
Make a free account to save your work and track your progress.
Extra: Try it: let the code find the edge
def is_teenager(age):
return age > 13 and age <= 19
low = 13
high = 19
for age in [low - 1, low, high, high + 1]:
should_pass = age >= low and age <= high
if is_teenager(age) == should_pass:
print(age, "PASS")
else:
print(age, "FAIL: wrong answer at this edge")
# One edge is wrong on purpose. Fix is_teenager so all four pass.
Make a free account to save your work and track your progress.
Match the example to the method
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Put the methods in order
Have a go
Put the methods in order
3 questionsBenefits (good points) and drawbacks (bad points)
| Method | Benefit | Drawback |
|---|---|---|
| Concept | Cheap: no code is needed | Users judge a sketch, not the real thing |
| Unit | Finds bugs early and shows exactly where | Does not show if parts work together |
| Boundary | Finds common edge mistakes, such as > (more than) where >= (more than or equal to) is needed | Tests only the edges, not everything |
| Integration | Finds problems where parts meet | Harder to find which part is at fault |
| System | Tests the real whole product | Late, so fixes cost more |
| Acceptance | The customer is happy it does what they need | Very late. A big change is expensive |
The cells students fill in are left blank in this preview.
Your turn: build test tools
Build three small testing tools.
boundary_values(low, high)gives back a list of the four boundary values: one below the lowest, the lowest, the highest and one above the highest.is_boundary(value, low, high)gives back True if a value is one of those four, and False if not.unit_result(actual, expected)gives back "PASS" if the two are equal and "FAIL" if they are different.
Press Run Tests to check.
def boundary_values(low, high):
# Give back a list of the four boundary values for a range:
# one below the lowest, the lowest, the highest, one above the highest.
# For 13 to 19 that is [12, 13, 19, 20].
return None
def is_boundary(value, low, high):
# Give back True if the value is one of the four boundary values for low to high.
# For 13 to 19, the values 12, 13, 19 and 20 give True. The value 16 gives False.
return None
def unit_result(actual, expected):
# A unit test compares what a part gave with what was expected.
# Give back "PASS" if they are equal, and "FAIL" if they are different.
return None
Make a free account to save your work, or press Start lab to work through the whole lesson.
Watch: testing parts, then the whole
Press Play on the short film below. It tests the fare function from this lesson, then the two functions from your task, first joined and then one part at a time. Watch which test shows where the bug is.
Film: testing parts, then the whole
Testing parts, then the whole
Every scene, as text
- A unit test checks one small part on its own: here the lesson's fare function. Each assert compares what fare gives with what was expected. The call fare(10) gives 1.0 and fare(40) gives 2.5, both as expected, so the program goes on and prints Both unit tests passed.
- Myth (false): you should test only the finished product. Here the task's boundary_values and is_boundary are joined, with one slip planted in the first, before either is tested, and only the whole is tried. The value 13 is the lowest of 13 to 19, a boundary value, so print(is_boundary(13, 13, 19)) should print True. It prints False. Something is wrong, but the result cannot say which part.
- The cure: test each part on its own. For 13 to 19, boundary_values should give [12, 13, 19, 20]. The line print(boundary_values(13, 19)) prints [12, 14, 19, 20], 14 where 13 should be, so the bug is in this part: low + 1, where the second value should be low.
- One change, low in place of low + 1, and the same unit test again. It prints [12, 13, 19, 20], the four boundary values: one below the lowest, the lowest, the highest and one above the highest. What it gave matches what was expected, so the unit test passes.
- Integration test: only when each part works are the parts joined. The function is_boundary calls boundary_values, where the two parts meet and pass data. The values 12, 13, 19 and 20 print True and 16 prints False, as the task's tests expect: the joined parts work together.
- Testing grows from small to big: a concept test of the idea, unit tests of each part, boundary tests of the edges, integration tests of the parts together, a system test of the whole thing, and last the acceptance test, by the customer.
Key words crossword
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
Exit quiz
Have a go