Software testing basics (unit tests)

Computer Science · Software engineering

Question 1 of 10

0

    A parameterized test:

    Stuck? Read the explanation

    Unlocks at a streak of 5 correct answers in a row.

    Print a free worksheet (PDF)

    Embed this practice on your site or class page

    Paste this HTML into your page. Students practise right there; it is free and needs no account.

    Software testing basics (unit tests) explained

    A unit test checks one small piece of code, such as a single function, in isolation by comparing its result with an expected value. Automated unit tests catch mistakes quickly and make it safer to change code later.

    A unit test usually follows three steps: arrange the inputs, act by calling the function, and assert that the output equals the expected value. A passing test shows that the function behaves as the test describes. It does not prove that the code is free of bugs.

    Good tests cover typical inputs, edge cases such as zero, empty lists or the largest value, and invalid inputs where the function should raise an error. Each test should check one behaviour and have a name that says what it checks, so a failing test points directly to the problem.

    Worked example

    A function apply_discount(price, percent) returns the price reduced by the given percentage. Two tests check it.

    assert apply_discount(80, 25) == 60
    assert apply_discount(80, 0) == 80
    
    1. Arrange: price 80 and percent 25. Act: the result is 80×(1−0.25)=80×0.75=6080 \times (1 - 0.25) = 80 \times 0.75 = 60. Assert: the result equals 60, so the test passes.
    2. Edge case: with percent 0 the result is 80×1=8080 \times 1 = 80, so the second test checks that no discount leaves the price unchanged.

    What you will practise

    104 multiple-choice questions (Easy, Medium, Hard). Each answer is followed by an explanation.

    Example questions

    • Testing verifies:
    • A unit test checks:
    • Test-driven development writes tests:
    • A test assertion:
    • Regression tests:
    • A test case is best described as:
    • A mock object is used in a test to:
    • A test that reproduces a reported bug is usually written:

    …and 96 more.