Projects · 11 / 19 Exercise Hard

Test bench

Objective

Create a Python program that works as a mini test bench: define three functions and automatically check a list of test cases.

Instructions

  • Define is_palindrome(text) (case-insensitive comparison), factorial(n) (raises ValueError if n is negative) and gcd(a, b) (Euclid's algorithm).
  • Read an integer n and then n lines with the format function arguments... expected, where function is palindromo, factorial or mcd, and expected is the expected result written as str() shows it (for example True or 120).
  • For each test, show Test k: OK if the result matches, Test k: FAIL (got X) if it does not, or Test k: ERROR (ExceptionType) if the function raises an exception (with its class name).
  • Finish with Passed: p of n.
Input
4
palindromo Ana True
factorial 5 120
mcd 12 18 5
factorial -1 1
Output
Test 1: OK
Test 2: OK
Test 3: FAIL (got 6)
Test 4: ERROR (ValueError)
Passed: 2 of 4

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

Start exercise