Practical software engineering foundations · Lesson 50 of 60 · Code lesson, JavaScript or Python · Optional
Testing
A test runs your code with a known input and checks that the result is what you expect.
A unit test checks one small piece, an integration test checks that pieces work together, and an end-to-end test checks the whole program the way a user would.
Passing tests give you the confidence to change code without fear.
Worked example
In JavaScript, a test compares the actual result with the expected result and reports the outcome.
function double(n) {
return n * 2;
}
console.log(double(4) === 8 ? "PASS" : "FAIL");Output: PASS
In Python, a test compares the actual result with the expected result and reports the outcome.
def double(n):
return n * 2
print("PASS" if double(4) == 8 else "FAIL")Output: PASS
Your turn
This tiny test runner checks isEven on three inputs and prints PASS or FAIL for each. All three fail. Read what the tests expect and fix isEven so all three print PASS.
Expected output:
PASS PASS PASS
Loading your practice…
Preparing your workspace…