checkcases 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checkcases-0.1.0/LICENSE +21 -0
- checkcases-0.1.0/PKG-INFO +96 -0
- checkcases-0.1.0/README.md +77 -0
- checkcases-0.1.0/pyproject.toml +28 -0
- checkcases-0.1.0/setup.cfg +4 -0
- checkcases-0.1.0/src/checkcases/__init__.py +179 -0
- checkcases-0.1.0/src/checkcases.egg-info/PKG-INFO +96 -0
- checkcases-0.1.0/src/checkcases.egg-info/SOURCES.txt +9 -0
- checkcases-0.1.0/src/checkcases.egg-info/dependency_links.txt +1 -0
- checkcases-0.1.0/src/checkcases.egg-info/top_level.txt +1 -0
- checkcases-0.1.0/tests/test_checkcases.py +77 -0
checkcases-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PyChallenge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: checkcases
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Check a Python function against example inputs with clear pass/fail output. Handy for practice exercises.
|
|
5
|
+
Author: PyChallenge
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://pychallenge.com
|
|
8
|
+
Project-URL: Practice challenges, https://pychallenge.com
|
|
9
|
+
Keywords: testing,exercises,practice,learning,education,examples
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Topic :: Education
|
|
14
|
+
Classifier: Topic :: Software Development :: Testing
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# checkcases
|
|
21
|
+
|
|
22
|
+
Check a Python function against a few example inputs and see clearly what passes and what fails.
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install checkcases
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Example
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from checkcases import check, raises
|
|
34
|
+
|
|
35
|
+
def fizzbuzz(n):
|
|
36
|
+
if n % 15 == 0:
|
|
37
|
+
return "FizzBuzz"
|
|
38
|
+
if n % 3 == 0:
|
|
39
|
+
return "Fizz"
|
|
40
|
+
if n % 5 == 0:
|
|
41
|
+
return "Buzz"
|
|
42
|
+
return str(n)
|
|
43
|
+
|
|
44
|
+
check(fizzbuzz, [
|
|
45
|
+
(3, "Fizz"),
|
|
46
|
+
(5, "Buzz"),
|
|
47
|
+
(15, "FizzBuzz"),
|
|
48
|
+
(7, "7"),
|
|
49
|
+
])
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
PASS fizzbuzz(3) == 'Fizz'
|
|
54
|
+
PASS fizzbuzz(5) == 'Buzz'
|
|
55
|
+
PASS fizzbuzz(15) == 'FizzBuzz'
|
|
56
|
+
PASS fizzbuzz(7) == '7'
|
|
57
|
+
------------------------------
|
|
58
|
+
4/4 passed
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
When something is wrong, you see the call, what came back and what you expected:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
FAIL add(1, 2) returned 4, expected 3
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Writing cases
|
|
68
|
+
|
|
69
|
+
| Case | Means |
|
|
70
|
+
|---|---|
|
|
71
|
+
| `(5, 25)` | `f(5)` should return `25` |
|
|
72
|
+
| `((2, 3), 5)` | `f(2, 3)` should return `5` |
|
|
73
|
+
| `{"args": (2,), "kwargs": {"b": 3}, "expected": 5}` | `f(2, b=3)` should return `5` |
|
|
74
|
+
| `((1, 0), raises(ZeroDivisionError))` | `f(1, 0)` should raise `ZeroDivisionError` |
|
|
75
|
+
|
|
76
|
+
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)`.
|
|
77
|
+
|
|
78
|
+
## Using the result
|
|
79
|
+
|
|
80
|
+
`check` returns a result you can use in scripts:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
result = check(add, cases, verbose=False)
|
|
84
|
+
if not result:
|
|
85
|
+
print(result.failures)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`result.passed`, `result.failed` and `result.failures` hold the details, and the result is truthy when everything passed.
|
|
89
|
+
|
|
90
|
+
## Looking for something to practice on?
|
|
91
|
+
|
|
92
|
+
[PyChallenge](https://pychallenge.com) has Python challenges to work through in your browser, from beginner to more advanced.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
# checkcases
|
|
2
|
+
|
|
3
|
+
Check a Python function against a few example inputs and see clearly what passes and what fails.
|
|
4
|
+
|
|
5
|
+
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.
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install checkcases
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Example
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from checkcases import check, raises
|
|
15
|
+
|
|
16
|
+
def fizzbuzz(n):
|
|
17
|
+
if n % 15 == 0:
|
|
18
|
+
return "FizzBuzz"
|
|
19
|
+
if n % 3 == 0:
|
|
20
|
+
return "Fizz"
|
|
21
|
+
if n % 5 == 0:
|
|
22
|
+
return "Buzz"
|
|
23
|
+
return str(n)
|
|
24
|
+
|
|
25
|
+
check(fizzbuzz, [
|
|
26
|
+
(3, "Fizz"),
|
|
27
|
+
(5, "Buzz"),
|
|
28
|
+
(15, "FizzBuzz"),
|
|
29
|
+
(7, "7"),
|
|
30
|
+
])
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
```
|
|
34
|
+
PASS fizzbuzz(3) == 'Fizz'
|
|
35
|
+
PASS fizzbuzz(5) == 'Buzz'
|
|
36
|
+
PASS fizzbuzz(15) == 'FizzBuzz'
|
|
37
|
+
PASS fizzbuzz(7) == '7'
|
|
38
|
+
------------------------------
|
|
39
|
+
4/4 passed
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
When something is wrong, you see the call, what came back and what you expected:
|
|
43
|
+
|
|
44
|
+
```
|
|
45
|
+
FAIL add(1, 2) returned 4, expected 3
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Writing cases
|
|
49
|
+
|
|
50
|
+
| Case | Means |
|
|
51
|
+
|---|---|
|
|
52
|
+
| `(5, 25)` | `f(5)` should return `25` |
|
|
53
|
+
| `((2, 3), 5)` | `f(2, 3)` should return `5` |
|
|
54
|
+
| `{"args": (2,), "kwargs": {"b": 3}, "expected": 5}` | `f(2, b=3)` should return `5` |
|
|
55
|
+
| `((1, 0), raises(ZeroDivisionError))` | `f(1, 0)` should raise `ZeroDivisionError` |
|
|
56
|
+
|
|
57
|
+
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)`.
|
|
58
|
+
|
|
59
|
+
## Using the result
|
|
60
|
+
|
|
61
|
+
`check` returns a result you can use in scripts:
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
result = check(add, cases, verbose=False)
|
|
65
|
+
if not result:
|
|
66
|
+
print(result.failures)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
`result.passed`, `result.failed` and `result.failures` hold the details, and the result is truthy when everything passed.
|
|
70
|
+
|
|
71
|
+
## Looking for something to practice on?
|
|
72
|
+
|
|
73
|
+
[PyChallenge](https://pychallenge.com) has Python challenges to work through in your browser, from beginner to more advanced.
|
|
74
|
+
|
|
75
|
+
## License
|
|
76
|
+
|
|
77
|
+
MIT
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "checkcases"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Check a Python function against example inputs with clear pass/fail output. Handy for practice exercises."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [{ name = "PyChallenge" }]
|
|
13
|
+
keywords = ["testing", "exercises", "practice", "learning", "education", "examples"]
|
|
14
|
+
classifiers = [
|
|
15
|
+
"Programming Language :: Python :: 3",
|
|
16
|
+
"Operating System :: OS Independent",
|
|
17
|
+
"Intended Audience :: Education",
|
|
18
|
+
"Topic :: Education",
|
|
19
|
+
"Topic :: Software Development :: Testing",
|
|
20
|
+
]
|
|
21
|
+
dependencies = []
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://pychallenge.com"
|
|
25
|
+
"Practice challenges" = "https://pychallenge.com"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools.packages.find]
|
|
28
|
+
where = ["src"]
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"""checkcases: check a function against example cases and see clear pass/fail output.
|
|
2
|
+
|
|
3
|
+
Made for practicing Python: write a function, list a few inputs with the
|
|
4
|
+
answers you expect, and run ``check``.
|
|
5
|
+
|
|
6
|
+
from checkcases import check
|
|
7
|
+
|
|
8
|
+
def add(a, b):
|
|
9
|
+
return a + b
|
|
10
|
+
|
|
11
|
+
check(add, [
|
|
12
|
+
((1, 2), 3),
|
|
13
|
+
((-1, 1), 0),
|
|
14
|
+
])
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import math
|
|
20
|
+
import sys
|
|
21
|
+
from dataclasses import dataclass, field
|
|
22
|
+
from typing import Any, Callable, Iterable
|
|
23
|
+
|
|
24
|
+
__all__ = ["check", "Case", "Result", "raises"]
|
|
25
|
+
__version__ = "0.1.0"
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
@dataclass(frozen=True)
|
|
29
|
+
class raises:
|
|
30
|
+
"""Expect the call to raise this exception type.
|
|
31
|
+
|
|
32
|
+
``check(f, [((0,), raises(ZeroDivisionError))])``
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
exc_type: type[BaseException]
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
@dataclass
|
|
39
|
+
class Case:
|
|
40
|
+
"""One example: positional args, keyword args and the expected result."""
|
|
41
|
+
|
|
42
|
+
args: tuple = ()
|
|
43
|
+
expected: Any = None
|
|
44
|
+
kwargs: dict = field(default_factory=dict)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
@dataclass
|
|
48
|
+
class Result:
|
|
49
|
+
passed: int
|
|
50
|
+
failed: int
|
|
51
|
+
failures: list[str]
|
|
52
|
+
|
|
53
|
+
@property
|
|
54
|
+
def ok(self) -> bool:
|
|
55
|
+
return self.failed == 0
|
|
56
|
+
|
|
57
|
+
def __bool__(self) -> bool:
|
|
58
|
+
return self.ok
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _to_case(item: Any) -> Case:
|
|
62
|
+
if isinstance(item, Case):
|
|
63
|
+
return item
|
|
64
|
+
if isinstance(item, dict):
|
|
65
|
+
return Case(
|
|
66
|
+
args=_as_args(item.get("args", ())),
|
|
67
|
+
expected=item.get("expected"),
|
|
68
|
+
kwargs=dict(item.get("kwargs", {})),
|
|
69
|
+
)
|
|
70
|
+
if isinstance(item, (tuple, list)) and len(item) == 2:
|
|
71
|
+
return Case(args=_as_args(item[0]), expected=item[1])
|
|
72
|
+
raise TypeError(
|
|
73
|
+
f"Can't read case {item!r}. Use (args, expected), a dict, or Case(...)."
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _as_args(value: Any) -> tuple:
|
|
78
|
+
# A single non-tuple value counts as one argument: (5, 25) means f(5) == 25.
|
|
79
|
+
return value if isinstance(value, tuple) else (value,)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def _equal(got: Any, expected: Any, tol: float) -> bool:
|
|
83
|
+
if isinstance(got, float) or isinstance(expected, float):
|
|
84
|
+
try:
|
|
85
|
+
return math.isclose(got, expected, rel_tol=tol, abs_tol=tol)
|
|
86
|
+
except TypeError:
|
|
87
|
+
return got == expected
|
|
88
|
+
if (
|
|
89
|
+
isinstance(got, (list, tuple))
|
|
90
|
+
and isinstance(expected, (list, tuple))
|
|
91
|
+
and type(got) is type(expected)
|
|
92
|
+
and len(got) == len(expected)
|
|
93
|
+
):
|
|
94
|
+
return all(_equal(g, e, tol) for g, e in zip(got, expected))
|
|
95
|
+
return got == expected
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _call_repr(name: str, case: Case) -> str:
|
|
99
|
+
parts = [repr(a) for a in case.args]
|
|
100
|
+
parts += [f"{k}={v!r}" for k, v in case.kwargs.items()]
|
|
101
|
+
return f"{name}({', '.join(parts)})"
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _supports_color(stream: Any) -> bool:
|
|
105
|
+
return hasattr(stream, "isatty") and stream.isatty()
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def check(
|
|
109
|
+
func: Callable[..., Any],
|
|
110
|
+
cases: Iterable[Any],
|
|
111
|
+
*,
|
|
112
|
+
tol: float = 1e-9,
|
|
113
|
+
verbose: bool = True,
|
|
114
|
+
stream: Any = None,
|
|
115
|
+
) -> Result:
|
|
116
|
+
"""Run ``func`` on every case and report which ones pass.
|
|
117
|
+
|
|
118
|
+
Each case can be:
|
|
119
|
+
* ``(args, expected)`` where args is a tuple, or a single value for one argument
|
|
120
|
+
* ``{"args": ..., "kwargs": {...}, "expected": ...}``
|
|
121
|
+
* ``Case(args, expected, kwargs)``
|
|
122
|
+
|
|
123
|
+
Use ``raises(SomeError)`` as the expected value when the call should fail.
|
|
124
|
+
Floats are compared with tolerance ``tol``. Returns a ``Result`` that is
|
|
125
|
+
truthy when every case passed.
|
|
126
|
+
"""
|
|
127
|
+
out = stream if stream is not None else sys.stdout
|
|
128
|
+
color = _supports_color(out)
|
|
129
|
+
green, red, dim, reset = (
|
|
130
|
+
("\033[32m", "\033[31m", "\033[2m", "\033[0m") if color else ("", "", "", "")
|
|
131
|
+
)
|
|
132
|
+
name = getattr(func, "__name__", "func")
|
|
133
|
+
passed = failed = 0
|
|
134
|
+
failures: list[str] = []
|
|
135
|
+
|
|
136
|
+
for item in cases:
|
|
137
|
+
case = _to_case(item)
|
|
138
|
+
call = _call_repr(name, case)
|
|
139
|
+
expected = case.expected
|
|
140
|
+
try:
|
|
141
|
+
got = func(*case.args, **case.kwargs)
|
|
142
|
+
except BaseException as exc: # noqa: BLE001 - we report every error
|
|
143
|
+
if isinstance(exc, (KeyboardInterrupt, SystemExit)):
|
|
144
|
+
raise
|
|
145
|
+
if isinstance(expected, raises) and isinstance(exc, expected.exc_type):
|
|
146
|
+
passed += 1
|
|
147
|
+
if verbose:
|
|
148
|
+
print(f"{green}PASS{reset} {call} raised {type(exc).__name__}", file=out)
|
|
149
|
+
continue
|
|
150
|
+
msg = f"{call} raised {type(exc).__name__}: {exc}"
|
|
151
|
+
failed += 1
|
|
152
|
+
failures.append(msg)
|
|
153
|
+
if verbose:
|
|
154
|
+
print(f"{red}FAIL{reset} {msg}", file=out)
|
|
155
|
+
continue
|
|
156
|
+
|
|
157
|
+
if isinstance(expected, raises):
|
|
158
|
+
msg = f"{call} returned {got!r}, expected it to raise {expected.exc_type.__name__}"
|
|
159
|
+
ok = False
|
|
160
|
+
else:
|
|
161
|
+
ok = _equal(got, expected, tol)
|
|
162
|
+
msg = f"{call} returned {got!r}, expected {expected!r}"
|
|
163
|
+
|
|
164
|
+
if ok:
|
|
165
|
+
passed += 1
|
|
166
|
+
if verbose:
|
|
167
|
+
print(f"{green}PASS{reset} {call} == {got!r}", file=out)
|
|
168
|
+
else:
|
|
169
|
+
failed += 1
|
|
170
|
+
failures.append(msg)
|
|
171
|
+
if verbose:
|
|
172
|
+
print(f"{red}FAIL{reset} {msg}", file=out)
|
|
173
|
+
|
|
174
|
+
total = passed + failed
|
|
175
|
+
if verbose:
|
|
176
|
+
summary = f"{passed}/{total} passed"
|
|
177
|
+
print(f"{dim}{'-' * 30}{reset}", file=out)
|
|
178
|
+
print(f"{green if failed == 0 else red}{summary}{reset}", file=out)
|
|
179
|
+
return Result(passed=passed, failed=failed, failures=failures)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: checkcases
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Check a Python function against example inputs with clear pass/fail output. Handy for practice exercises.
|
|
5
|
+
Author: PyChallenge
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://pychallenge.com
|
|
8
|
+
Project-URL: Practice challenges, https://pychallenge.com
|
|
9
|
+
Keywords: testing,exercises,practice,learning,education,examples
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Intended Audience :: Education
|
|
13
|
+
Classifier: Topic :: Education
|
|
14
|
+
Classifier: Topic :: Software Development :: Testing
|
|
15
|
+
Requires-Python: >=3.8
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Dynamic: license-file
|
|
19
|
+
|
|
20
|
+
# checkcases
|
|
21
|
+
|
|
22
|
+
Check a Python function against a few example inputs and see clearly what passes and what fails.
|
|
23
|
+
|
|
24
|
+
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.
|
|
25
|
+
|
|
26
|
+
```bash
|
|
27
|
+
pip install checkcases
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Example
|
|
31
|
+
|
|
32
|
+
```python
|
|
33
|
+
from checkcases import check, raises
|
|
34
|
+
|
|
35
|
+
def fizzbuzz(n):
|
|
36
|
+
if n % 15 == 0:
|
|
37
|
+
return "FizzBuzz"
|
|
38
|
+
if n % 3 == 0:
|
|
39
|
+
return "Fizz"
|
|
40
|
+
if n % 5 == 0:
|
|
41
|
+
return "Buzz"
|
|
42
|
+
return str(n)
|
|
43
|
+
|
|
44
|
+
check(fizzbuzz, [
|
|
45
|
+
(3, "Fizz"),
|
|
46
|
+
(5, "Buzz"),
|
|
47
|
+
(15, "FizzBuzz"),
|
|
48
|
+
(7, "7"),
|
|
49
|
+
])
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
PASS fizzbuzz(3) == 'Fizz'
|
|
54
|
+
PASS fizzbuzz(5) == 'Buzz'
|
|
55
|
+
PASS fizzbuzz(15) == 'FizzBuzz'
|
|
56
|
+
PASS fizzbuzz(7) == '7'
|
|
57
|
+
------------------------------
|
|
58
|
+
4/4 passed
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
When something is wrong, you see the call, what came back and what you expected:
|
|
62
|
+
|
|
63
|
+
```
|
|
64
|
+
FAIL add(1, 2) returned 4, expected 3
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Writing cases
|
|
68
|
+
|
|
69
|
+
| Case | Means |
|
|
70
|
+
|---|---|
|
|
71
|
+
| `(5, 25)` | `f(5)` should return `25` |
|
|
72
|
+
| `((2, 3), 5)` | `f(2, 3)` should return `5` |
|
|
73
|
+
| `{"args": (2,), "kwargs": {"b": 3}, "expected": 5}` | `f(2, b=3)` should return `5` |
|
|
74
|
+
| `((1, 0), raises(ZeroDivisionError))` | `f(1, 0)` should raise `ZeroDivisionError` |
|
|
75
|
+
|
|
76
|
+
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)`.
|
|
77
|
+
|
|
78
|
+
## Using the result
|
|
79
|
+
|
|
80
|
+
`check` returns a result you can use in scripts:
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
result = check(add, cases, verbose=False)
|
|
84
|
+
if not result:
|
|
85
|
+
print(result.failures)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
`result.passed`, `result.failed` and `result.failures` hold the details, and the result is truthy when everything passed.
|
|
89
|
+
|
|
90
|
+
## Looking for something to practice on?
|
|
91
|
+
|
|
92
|
+
[PyChallenge](https://pychallenge.com) has Python challenges to work through in your browser, from beginner to more advanced.
|
|
93
|
+
|
|
94
|
+
## License
|
|
95
|
+
|
|
96
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
checkcases
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import unittest
|
|
3
|
+
|
|
4
|
+
from checkcases import Case, check, raises
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def add(a, b):
|
|
8
|
+
return a + b
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def square(x):
|
|
12
|
+
return x * x
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def divide(a, b):
|
|
16
|
+
return a / b
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class CheckTests(unittest.TestCase):
|
|
20
|
+
def run_check(self, func, cases, **kw):
|
|
21
|
+
buf = io.StringIO()
|
|
22
|
+
return check(func, cases, stream=buf, **kw), buf.getvalue()
|
|
23
|
+
|
|
24
|
+
def test_all_pass(self):
|
|
25
|
+
r, out = self.run_check(add, [((1, 2), 3), ((0, 0), 0)])
|
|
26
|
+
self.assertTrue(r)
|
|
27
|
+
self.assertEqual(r.passed, 2)
|
|
28
|
+
self.assertIn("2/2 passed", out)
|
|
29
|
+
|
|
30
|
+
def test_failure_reported(self):
|
|
31
|
+
r, out = self.run_check(add, [((1, 2), 4)])
|
|
32
|
+
self.assertFalse(r)
|
|
33
|
+
self.assertIn("add(1, 2) returned 3, expected 4", out)
|
|
34
|
+
|
|
35
|
+
def test_single_argument_shortcut(self):
|
|
36
|
+
r, _ = self.run_check(square, [(5, 25), (-3, 9)])
|
|
37
|
+
self.assertTrue(r)
|
|
38
|
+
|
|
39
|
+
def test_float_tolerance(self):
|
|
40
|
+
r, _ = self.run_check(add, [((0.1, 0.2), 0.3)])
|
|
41
|
+
self.assertTrue(r)
|
|
42
|
+
|
|
43
|
+
def test_expected_exception(self):
|
|
44
|
+
r, _ = self.run_check(divide, [((1, 0), raises(ZeroDivisionError))])
|
|
45
|
+
self.assertTrue(r)
|
|
46
|
+
|
|
47
|
+
def test_unexpected_exception_is_failure(self):
|
|
48
|
+
r, out = self.run_check(divide, [((1, 0), 1)])
|
|
49
|
+
self.assertFalse(r)
|
|
50
|
+
self.assertIn("ZeroDivisionError", out)
|
|
51
|
+
|
|
52
|
+
def test_missing_exception_is_failure(self):
|
|
53
|
+
r, _ = self.run_check(divide, [((4, 2), raises(ZeroDivisionError))])
|
|
54
|
+
self.assertFalse(r)
|
|
55
|
+
|
|
56
|
+
def test_kwargs_and_dict_cases(self):
|
|
57
|
+
r, _ = self.run_check(
|
|
58
|
+
add,
|
|
59
|
+
[
|
|
60
|
+
{"args": (1,), "kwargs": {"b": 2}, "expected": 3},
|
|
61
|
+
Case(args=(2,), kwargs={"b": 3}, expected=5),
|
|
62
|
+
],
|
|
63
|
+
)
|
|
64
|
+
self.assertTrue(r)
|
|
65
|
+
|
|
66
|
+
def test_quiet_mode(self):
|
|
67
|
+
r, out = self.run_check(add, [((1, 2), 3)], verbose=False)
|
|
68
|
+
self.assertTrue(r)
|
|
69
|
+
self.assertEqual(out, "")
|
|
70
|
+
|
|
71
|
+
def test_bad_case_shape(self):
|
|
72
|
+
with self.assertRaises(TypeError):
|
|
73
|
+
self.run_check(add, [(1, 2, 3)])
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
if __name__ == "__main__":
|
|
77
|
+
unittest.main()
|