Skip to main content
ThePythonBook/Assessment

Testing Assessment

Prove your testing skills by writing test functions, fixing broken assertions, predicting test outcomes, and refactoring code for testability. All exercises use plain assert statements compatible with in-browser Python.

Progress
790 XP0/10
#1Write Tests for a Calculator
Write Code

Write a function called test_calculator that tests the calculate function shown below. Your test function should use assert statements to verify:

  • Addition: calculate(10, 5, "+") returns 15
  • Subtraction: calculate(10, 5, "-") returns 5
  • Multiplication: calculate(3, 4, "*") returns 12
  • Division: calculate(10, 2, "/") returns 5.0
  • Division by zero: calculate(10, 0, "/") returns "Error: division by zero"
  • The calculate function is already provided. You only need to write test_calculator.

    Loading editor...
    #2Write Edge-Case Tests for a String Utility
    Write Code

    Write a function called test_truncate that tests the truncate function below with edge cases. Your tests must assert all of the following:

  • A short string (shorter than max_length) is returned unchanged.
  • A long string is truncated and ends with "...".
  • The returned string (including "...") is never longer than max_length.
  • An empty string returns an empty string.
  • When max_length is 3, a long string returns "...".
  • Loading editor...
    #3Write a Testable Validator
    Write Code

    Write a function validate_password(password) that returns a list of error message strings. The rules are:

  • If the password is shorter than 8 characters, include "Too short".
  • If the password has no uppercase letter, include "No uppercase".
  • If the password has no digit, include "No digit".
  • If all rules pass, return an empty list.
  • Returning a list of errors (instead of printing or raising) makes the function easy to test.

    Loading editor...
    #4Fix the Broken Assertion
    Fix the Bug

    The test function below has incorrect assertions. The average function works correctly, but the tests fail because the expected values are wrong. Fix the assertions so all tests pass.

    Do not change the average function -- only fix the test assertions.

    Loading editor...
    #5Fix the Test That Mutates Shared State
    Fix the Bug

    These two test functions share a mutable default list and interfere with each other. The second test fails because the first test mutated the shared list.

    Fix the code so that each test works with its own independent copy of the data. Do not change the assertions -- only fix how data is set up.

    Loading editor...
    #6Fix the Exception Test
    Fix the Bug

    The test_divide_by_zero function is supposed to verify that divide(10, 0) raises a ValueError. However, the test is written incorrectly and does not actually catch or verify the exception.

    Fix the test so it properly asserts that a ValueError is raised when dividing by zero. Without pytest, use a try/except pattern.

    Loading editor...
    #7Predict the Test Output
    Predict Output

    Read the code below carefully. What does it print? Think about what each assertion does and which one (if any) will fail.

    Enter the exact output that this code produces.

    Loading editor...
    #8Predict Which Test Fails
    Predict Output

    Read the code below. One of the assertions will fail. What is the exact output including the error message?

    Hint: Python stops at the first failed assertion and prints an AssertionError.

    Loading editor...
    #9Refactor: Extract Testable Logic
    Refactor

    The function process_order below mixes validation, calculation, and printing. Refactor it into three separate, testable functions:

    1. validate_order(items, quantities) -- returns True if both lists are non-empty and the same length, False otherwise.

    2. calculate_total(items, quantities, prices) -- returns the total cost as a float.

    3. format_receipt(items, quantities, prices, total) -- returns a string: one line per item ("{item} x{qty}: ${cost:.2f}") followed by a final line "Total: ${total:.2f}".

    Keep the logic the same, just split it up. The prices dict is provided.

    Loading editor...
    #10Refactor: Replace Hard-Coded Dependency with Injection
    Refactor

    The get_greeting function has a hard-coded dependency on get_current_hour, which makes it impossible to test deterministically. Refactor get_greeting so it accepts an hour parameter instead of calling get_current_hour internally.

    Rules:

  • hour < 12 returns "Good morning"
  • 12 <= hour < 18 returns "Good afternoon"
  • hour >= 18 returns "Good evening"
  • After refactoring, write a function test_greeting() that asserts all three time ranges work.

    Loading editor...