Testing a function with expected and actual output

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Testing a function with expected and actual output

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line compares the actual output of square(5) with the expected value?
A console.log("expected 25, got", square(5));
B console.log("square(5)");
2. square(5) logs "expected 25, got 10". What is the test result?
A Pass, because the function ran without an error
B Fail, because the actual value does not match the expected value
3. Which is the best way to choose the expected value for a test?
A Work it out by hand before you run the function
B Copy whatever the function prints and call it expected
4. Which set of inputs is the better test of a function that checks if a mark is a pass (50 or more)?
A 80, 81 and 82 only
B 49, 50 and 51 (boundary values) plus a normal value like 80
5. You fix a bug in your function. What should you do next?
A Run all the tests again, including the ones that passed before
B Only rerun the test that failed, because the others are fine

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1console.log("expected 25, got", square(5)); Asquare is not called. Without brackets and an input like (5), it logs the function itself instead of the result.
2console.log("expected 0, got", square(0)); BThe actual result is typed in by hand, so the function is never called and the test proves nothing.
3Test a function with a normal input and log the expected value beside the actual output Cconsole.log("retest: expected 25, got", square(5));
4After fixing a bug, run the same test again to check the fix worked DCalls square with the input 5 and prints the actual result next to the expected value 25 so you can compare them.
5console.log("expected 25, got", square); Econsole.log("expected 36, got", square(6));
6console.log("expected 25, got 25"); FTests a boundary input: 0 is an edge case, and the expected result is written beside the actual result.

Definitions

console.log("expected 25, got", square(5))

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
console.log("expected 25, got", ______(5));
2.
console.log("expected 25, got", square(_));
3.
console.log("expected 0, got", square(_));
4.
let actual = square(4); if (actual ___ 16) console.log("pass");
5.
function square(n) { return n _ n; } console.log("expected 25, got", square(5));

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A function sq(n) {
B return n * n;
C }
D console.log("want 25", sq);
Question 2
A function sq(n) {
B return n + n;
C }
D let got = sq(5);
E console.log("want 25", got);
Question 3
A function sq(n) {return n*n;}
B let got = sq(5);
C if (got = 25) {
D console.log("pass");
E }
Question 4
A function isAdult(age) {
B return age >= 18;
C }
D let got = isAdult(17);
E console.log("want true", got);
Question 5
A function sq(n) {return n*n;}
B let got = sq(6);
C console.log("want 36", get);

Answer Key

Testing a function with expected and actual output
Section A: Quick Questions
Q1
A — console.log("expected 25, got", square(5));
Q2
B — Fail, because the actual value does not match the expected value
Q3
A — Work it out by hand before you run the function
Q4
B — 49, 50 and 51 (boundary values) plus a normal value like 80
Q5
A — Run all the tests again, including the ones that passed before
Section B: Match the Pairs
Q1
console.log("expected 25, got", square(5)); → Calls square with the input 5 and prints the actual result next to the expected value 25 so you can compare them.
Q2
console.log("expected 0, got", square(0)); → Tests a boundary input: 0 is an edge case, and the expected result is written beside the actual result.
Q3
Test a function with a normal input and log the expected value beside the actual output → console.log("expected 36, got", square(6));
Q4
After fixing a bug, run the same test again to check the fix worked → console.log("retest: expected 25, got", square(5));
Q5
console.log("expected 25, got", square); → square is not called. Without brackets and an input like (5), it logs the function itself instead of the result.
Q6
console.log("expected 25, got 25"); → The actual result is typed in by hand, so the function is never called and the test proves nothing.
Section C: Definitions
Q1
console.log("expected 25, got", square(5)) — Test a function by calling it with chosen inputs, logging the actual result next to the expected value, then recording pass or fail. Retest after any fix.
Section D: Fill in the Blanks
Q1
console.log("expected 25, got", square(5));
Q2
console.log("expected 25, got", square(5));
Q3
console.log("expected 0, got", square(0));
Q4
let actual = square(4); if (actual === 16) console.log("pass");
Q5
function square(n) { return n * n; } console.log("expected 25, got", square(5));
Section E: Spot the Error
Q1
D — sq has no brackets or input, so the function is not called. The test prints the function itself, not the actual result. It should be sq(5).
Q2
B — Squaring means multiplying n by itself. With n + n, sq(5) gives 10, so the test would show 10 next to the expected 25 and fail. It should be return n * n;
Q3
C — A single = assigns 25 to got instead of comparing, so the test always 'passes'. Use === to compare the actual and expected values: if (got === 25) {
Q4
D — The test expects true for the boundary input 18. Calling isAdult(17) returns false, so the test does not check the boundary it claims to. It should be isAdult(18).
Q5
C — The variable is called got, not get. Using get causes a ReferenceError, so no actual value is shown. It should be console.log("want 36", got);