72 lines
1.7 KiB
Python
72 lines
1.7 KiB
Python
import pytest
|
|
|
|
# Paramètres simples avec id au bout
|
|
@pytest.mark.parametrize("x", [
|
|
pytest.param(1, id="one"),
|
|
pytest.param(2, id="two")
|
|
])
|
|
def test_basic_ids(x):
|
|
pass
|
|
|
|
# id au milieu avec autre kwarg (reason)
|
|
@pytest.mark.parametrize("x", [
|
|
pytest.param(10, id="ten"),
|
|
pytest.param(20, marks=pytest.mark.skip(reason="nope"), id="twenty"),
|
|
])
|
|
def test_with_reason_and_marks(x):
|
|
pass
|
|
|
|
# Plusieurs arguments positionnels
|
|
@pytest.mark.parametrize("a, b", [
|
|
pytest.param(1, 2, id="1-2"),
|
|
pytest.param(3, 4, id="3-4"),
|
|
])
|
|
def test_multiple_positional_args(a, b):
|
|
pass
|
|
|
|
# Avec un argument non-évaluable
|
|
@pytest.mark.parametrize("data", [
|
|
pytest.param(object(), id="custom-obj"),
|
|
])
|
|
def test_non_literal_with_id(data):
|
|
pass
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
testdata = [
|
|
(datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)),
|
|
(datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)),
|
|
]
|
|
|
|
@pytest.mark.parametrize("a,b,expected", testdata)
|
|
def test_timedistance_v1(a, b, expected):
|
|
diff = a - b
|
|
assert diff == expected
|
|
|
|
|
|
scenarios = [
|
|
("one", {"x": 1, "y": 2}),
|
|
("two", {"x": 3, "y": 4}),
|
|
("edge", {"x": -1, "y": -1}),
|
|
]
|
|
|
|
def pytest_generate_tests(metafunc):
|
|
if hasattr(metafunc.cls, "scenarios"):
|
|
ids = []
|
|
argnames = []
|
|
argvalues = []
|
|
|
|
for name, param_dict in metafunc.cls.scenarios:
|
|
ids.append(name)
|
|
if not argnames:
|
|
argnames = list(param_dict.keys())
|
|
argvalues.append([param_dict[k] for k in argnames])
|
|
|
|
metafunc.parametrize(argnames, argvalues, ids=ids)
|
|
|
|
|
|
class TestDynamic:
|
|
scenarios = scenarios
|
|
|
|
def test_sum_positive(self, x, y):
|
|
assert (x + y) >= 0 |