From af99cb3356d009e2fd4bc5e6b1f0fedb8b3dd83a Mon Sep 17 00:00:00 2001 From: tligui_y Date: Sun, 20 Jul 2025 23:07:22 +0200 Subject: [PATCH] Update tests/test_param.py --- tests/test_param.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/tests/test_param.py b/tests/test_param.py index 4143bf77a..20282c1e9 100644 --- a/tests/test_param.py +++ b/tests/test_param.py @@ -1,6 +1,6 @@ import pytest -# Paramètres simples avec id au bout +# Parameters with id @pytest.mark.parametrize("x", [ pytest.param(1, id="one"), pytest.param(2, id="two") @@ -8,7 +8,7 @@ import pytest def test_basic_ids(x): pass -# id au milieu avec autre kwarg (reason) +# 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"), @@ -16,7 +16,7 @@ def test_basic_ids(x): def test_with_reason_and_marks(x): pass -# Plusieurs arguments positionnels +# Parametrize with many arguments @pytest.mark.parametrize("a, b", [ pytest.param(1, 2, id="1-2"), pytest.param(3, 4, id="3-4"), @@ -24,13 +24,16 @@ def test_with_reason_and_marks(x): def test_multiple_positional_args(a, b): pass -# Avec un argument non-évaluable +# 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 = [ @@ -43,7 +46,7 @@ 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}), @@ -71,7 +74,7 @@ class TestDynamic: def test_sum_positive(self, 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"]) @@ -79,8 +82,8 @@ def pytest_generate_tests(metafunc): def test_dynamic(val): assert val % 10 == 0 -import pytest +# Use of pytest.fixture to parametrize the test @pytest.fixture(params=["fire", "water", "earth"], ids=["🔥", "💧", "🌍"]) def element(request): return request.param @@ -88,11 +91,13 @@ def element(request): 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 @@ -102,6 +107,7 @@ def complex_setup(request): 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 \ No newline at end of file