Skip to content

Commit 12559db

Browse files
author
Tobias Deiminger
committed
Add and adapt tests
Add tests to assert #8914 is fixed. Tests assures that both reordering and caching work as intended, and demonstrates how one can use parameter ids to decide about equality. Adapting issue_519.checked_order is not cheating. See our discussion at #9350 (review)
1 parent b891a92 commit 12559db

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-2
lines changed

testing/example_scripts/issue_519.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def checked_order():
2222
assert order == [
2323
("issue_519.py", "fix1", "arg1v1"),
2424
("test_one[arg1v1-arg2v1]", "fix2", "arg2v1"),
25-
("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"),
2625
("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"),
26+
("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"),
2727
("test_two[arg1v1-arg2v2]", "fix2", "arg2v2"),
2828
("issue_519.py", "fix1", "arg1v2"),
2929
("test_one[arg1v2-arg2v1]", "fix2", "arg2v1"),
30-
("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"),
3130
("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"),
31+
("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"),
3232
("test_two[arg1v2-arg2v2]", "fix2", "arg2v2"),
3333
]
3434

testing/python/fixtures.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,59 @@ def test2(no_eq):
13081308
result = pytester.runpytest()
13091309
result.stdout.fnmatch_lines(["*4 passed*"])
13101310

1311+
@pytest.mark.parametrize(
1312+
("parametrize1", "parametrize2"),
1313+
[
1314+
(
1315+
'"fix", [1, 2], indirect=True',
1316+
'"fix", [2, 1], indirect=True',
1317+
),
1318+
(
1319+
'"fix", [1, pytest.param({"data": 2}, id="2")], indirect=True',
1320+
'"fix", [pytest.param({"data": 2}, id="2"), 1], indirect=True',
1321+
),
1322+
(
1323+
'"fix", [{"data": 1}, {"data": 2}], indirect=True, ids=lambda d: MyEnum(d["data"])',
1324+
'"fix", [{"data": 2}, {"data": 1}], indirect=True, ids=lambda d: MyEnum(d["data"])',
1325+
),
1326+
(
1327+
'"fix", [{"data": 1}, {"data": 2}], indirect=True, ids=[1, "two"]',
1328+
'"fix", [{"data": 2}, {"data": 1}], indirect=True, ids=["two", 1]',
1329+
),
1330+
],
1331+
)
1332+
def test_reorder_and_cache(
1333+
self, pytester: Pytester, parametrize1, parametrize2
1334+
) -> None:
1335+
"""Test optimization for minimal setup/teardown with indirectly parametrized fixtures. See #8914, #9420."""
1336+
pytester.makepyfile(
1337+
f"""
1338+
import pytest
1339+
from enum import Enum
1340+
class MyEnum(Enum):
1341+
Id1 = 1
1342+
Id2 = 2
1343+
@pytest.fixture(scope="session")
1344+
def fix(request):
1345+
value = request.param["data"] if isinstance(request.param, dict) else request.param
1346+
print(f'prepare foo-%s' % value)
1347+
yield value
1348+
print(f'teardown foo-%s' % value)
1349+
@pytest.mark.parametrize({parametrize1})
1350+
def test1(fix):
1351+
pass
1352+
@pytest.mark.parametrize({parametrize2})
1353+
def test2(fix):
1354+
pass
1355+
"""
1356+
)
1357+
result = pytester.runpytest("-s")
1358+
output = result.stdout.str()
1359+
assert output.count("prepare foo-1") == 1
1360+
assert output.count("prepare foo-2") == 1
1361+
assert output.count("teardown foo-1") == 1
1362+
assert output.count("teardown foo-2") == 1
1363+
13111364
def test_funcarg_parametrized_and_used_twice(self, pytester: Pytester) -> None:
13121365
pytester.makepyfile(
13131366
"""

0 commit comments

Comments
 (0)