parametrize in Python
One test body, a row of cases, and a separate result for each.
import pytest
def wpm(chars, seconds):
return (chars / 5) / (seconds / 60)
@pytest.mark.parametrize("chars,seconds,expected", [
(400, 60, 80.0),
(200, 60, 40.0),
(400, 30, 160.0),
])
def test_wpm(chars, seconds, expected):
assert wpm(chars, seconds) == pytest.approx(expected)
@pytest.mark.parametrize("chars", [0, 100])
@pytest.mark.parametrize("seconds", [30, 60])
def test_never_negative(chars, seconds):
assert wpm(chars, seconds) >= 0
How it works
- The ids in the report come from the parameters.
- Several parameters multiply into a grid.
- It replaces a loop inside a single test, which hides failures.
Keywords and builtins used here
assertdefreturntest_never_negativetest_wpmwpm
The run, in numbers
- Lines
- 20
- Characters to type
- 445
- Tokens
- 133
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 80 seconds.
Step 3 of 3 in Writing tests, step 3 of 9 in Testing with pytest.