101 lines
2.7 KiB
Python
101 lines
2.7 KiB
Python
import pytest
|
|
|
|
# Parameters with id
|
|
@pytest.mark.parametrize("x", [
|
|
pytest.param(1, id="one"),
|
|
pytest.param(2, id="two")
|
|
])
|
|
def test_basic_ids(x):
|
|
pass
|
|
|
|
# One test will pass, the other be skipped because of the mark content
|
|
@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
|
|
|
|
# Parametrize with many arguments
|
|
@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
|
|
|
|
# Not assessable argument : object
|
|
@pytest.mark.parametrize("data", [
|
|
pytest.param(object(), id="custom-obj"),
|
|
])
|
|
def test_non_literal_with_id(data):
|
|
pass
|
|
|
|
|
|
# Parameters parametrize passed indirectly
|
|
|
|
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
|
|
|
|
# Dynamically generates test cases using scenarios
|
|
scenarios = [
|
|
("one", {"x": 1, "y": 2}),
|
|
("two", {"x": 3, "y": 4}),
|
|
("edge", {"x": -1, "y": -1}),
|
|
]
|
|
|
|
@pytest.mark.parametrize("x, y", [(param["x"], param["y"]) for _, param in scenarios], ids=[name for name, _ in scenarios])
|
|
def test_sum_positive(x, y):
|
|
assert (x + y) >= 0
|
|
|
|
|
|
# Uses metafunc.parametrize to dynamically parameterize
|
|
def pytest_generate_tests(metafunc):
|
|
if "val" in metafunc.fixturenames:
|
|
metafunc.parametrize("val", [10, 20, 30], ids=["ten", "twenty", "thirty"])
|
|
|
|
def test_dynamic(val):
|
|
assert val % 10 == 0
|
|
|
|
|
|
# Use of pytest.fixture to parametrize the test
|
|
@pytest.fixture(params=["fire", "water", "earth"], ids=["🔥", "💧", "🌍"])
|
|
def element(request):
|
|
return request.param
|
|
|
|
def test_element_type(element):
|
|
assert isinstance(element, str)
|
|
|
|
# Parametrize test with two parametrize set of values
|
|
@pytest.mark.parametrize("x", [1, 2])
|
|
@pytest.mark.parametrize("y", ["a", "b"])
|
|
def test_combination(x, y):
|
|
assert isinstance(y, str)
|
|
|
|
# Defines a fixture `complex_setup` to parametrize the test with dynamic values
|
|
@pytest.fixture
|
|
def complex_setup(request):
|
|
val = request.param
|
|
return val * 2
|
|
|
|
@pytest.mark.parametrize("complex_setup", [1, 2], indirect=True)
|
|
def test_indirect_fixture(complex_setup):
|
|
assert complex_setup in [2, 4]
|
|
|
|
# Parameters in function arguments
|
|
def test_addition(a = 5, b = 5, expected = 10):
|
|
# Has to pass
|
|
assert addition(a, b) == expected
|
|
|
|
# Test using a none defined fixture
|
|
def test_basic_error(x):
|
|
assert x == 10 |