I have parametrized test, for example:
import pytest
from datetime import time
def get_next_hour(time_str: str):
t = time(*map(int, time_str.split(':')))
new_t = time((t.hour + 1) % 24, t.minute)
return new_t.strftime('%H:%M')
@pytest.mark.parametrize(
"t_str, result_str",
[
('10:30', '11:30'),
('11:30', '12:30'),
]
)
def test_good_input(t_str, result_str):
result = get_next_hour(t_str)
assert result == result_str
Test test_good_input must work only with valid time strings (for invalid data I have another test test_bad_input). How I can emphasize it - in docstring, or using assert for input data?
With docstring
def test_good_input(t_str, result_str):
"""for t_str allowed only time-valid strings"""
result = get_next_hour(t_str)
assert result == result_str
With validation input
def test_good_input(t_str, result_str):
assert ':' in t_str, 'input data is not time'
result = get_next_hour(t_str)
assert result == result_str
Or there is another ways?
I'd say a fat warning in the comment or the test function docstring should be enough. This is not an untrusted user input that should be validated; also, it's better to keep the tests as simple as possible. If some dev misuses the test inputs without reading the docs first, it's his own fault now.
However, test args validation is surely possible with
pytest(e.g. to inform the devs that there's nothing wrong with the tested function and they are using the test wrong). I would do an implicit args validation by using the indirect parametrization. In the below example, each arg frommark.parametrizewill be first passed to a fixture with the same name where you can do preprocessing before the test starts:Now the third test will fail with a descriptive error message:
More info on indirect parametrization: Deferring the setup of parametrized resources