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.
Write a function called test_calculator that tests the calculate function shown below. Your test function should use assert statements to verify:
calculate(10, 5, "+") returns 15calculate(10, 5, "-") returns 5calculate(3, 4, "*") returns 12calculate(10, 2, "/") returns 5.0calculate(10, 0, "/") returns "Error: division by zero"The calculate function is already provided. You only need to write test_calculator.
Write a function called test_truncate that tests the truncate function below with edge cases. Your tests must assert all of the following:
"..."."...") is never longer than max_length.max_length is 3, a long string returns "...".Write a function validate_password(password) that returns a list of error message strings. The rules are:
"Too short"."No uppercase"."No digit".Returning a list of errors (instead of printing or raising) makes the function easy to test.
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.
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.
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.
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.
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.
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.
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.