checkcases
Check a Python function against a few example inputs and see clearly what passes and what fails.
It's made for practice: you write a function, list some inputs with the answers you expect, and run check. No test framework, no setup, no dependencies.
pip install checkcases
Example
from checkcases import check, raises
def fizzbuzz(n):
if n % 15 == 0:
return "FizzBuzz"
if n % 3 == 0:
return "Fizz"
if n % 5 == 0:
return "Buzz"
return str(n)
check(fizzbuzz, [
(3, "Fizz"),
(5, "Buzz"),
(15, "FizzBuzz"),
(7, "7"),
])
PASS fizzbuzz(3) == 'Fizz'
PASS fizzbuzz(5) == 'Buzz'
PASS fizzbuzz(15) == 'FizzBuzz'
PASS fizzbuzz(7) == '7'
------------------------------
4/4 passed
When something is wrong, you see the call, what came back and what you expected:
FAIL add(1, 2) returned 4, expected 3
Writing cases
| Case |
Means |
(5, 25) |
f(5) should return 25 |
((2, 3), 5) |
f(2, 3) should return 5 |
{"args": (2,), "kwargs": {"b": 3}, "expected": 5} |
f(2, b=3) should return 5 |
((1, 0), raises(ZeroDivisionError)) |
f(1, 0) should raise ZeroDivisionError |
Floats are compared with a small tolerance, so 0.1 + 0.2 counts as 0.3. Change it with check(f, cases, tol=1e-6).
Using the result
check returns a result you can use in scripts:
result = check(add, cases, verbose=False)
if not result:
print(result.failures)
result.passed, result.failed and result.failures hold the details, and the result is truthy when everything passed.
Looking for something to practice on?
PyChallenge has Python challenges to work through in your browser, from beginner to more advanced.
License
MIT