Debugging and testing · 8 / 14 Exercise Easy

Testing with assert

Objective

Create a Python program that tests a function with a list of cases using assert.

Instructions

  • Define is_leap(year), which returns True if the year is divisible by 4 and not by 100, or if it is divisible by 400.
  • Read an integer n and then n lines with the format year expected_result, where the expected result is True or False.
  • For each case, inside a try, check assert is_leap(year) == expected. If it holds show Test K: OK and if AssertionError is raised, Test K: FAIL. K starts at 1.
  • At the end show Failures: F.
Input
4
2000 True
1900 False
2024 True
2023 False
Output
Test 1: OK
Test 2: OK
Test 3: OK
Test 4: OK
Failures: 0

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise