Skip to content

Commit a00c9b1

Browse files
author
Tobias Deiminger
committed
Add and adapt tests
Add a test to assert pytest-dev#8914 is fixed. The test 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 pytest-dev#9350 (review)
1 parent 57ea297 commit a00c9b1

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1308,6 +1308,32 @@ def test2(no_eq):
13081308
result = pytester.runpytest()
13091309
result.stdout.fnmatch_lines(["*4 passed*"])
13101310

1311+
def test_optimize_by_reorder_indirect(self, pytester: Pytester) -> None:
1312+
"""Test reordering for minimal setup/teardown with indirectly parametrized fixtures. See #8914, #9350."""
1313+
pytester.makepyfile(
1314+
"""
1315+
import pytest
1316+
@pytest.fixture(scope="session")
1317+
def fix(request):
1318+
value = request.param["data"] if isinstance(request.param, dict) else request.param
1319+
print(f'prepare foo-%s' % value)
1320+
yield value
1321+
print(f'teardown foo-%s' % value)
1322+
@pytest.mark.parametrize("fix", [1, pytest.param({"data": 2}, id="2")], indirect=True)
1323+
def test1(fix):
1324+
pass
1325+
@pytest.mark.parametrize("fix", [pytest.param({"data": 2}, id="2"), 1], indirect=True)
1326+
def test2(fix):
1327+
pass
1328+
"""
1329+
)
1330+
result = pytester.runpytest("-s")
1331+
output = result.stdout.str()
1332+
assert output.count("prepare foo-1") == 1
1333+
assert output.count("prepare foo-2") == 1
1334+
assert output.count("teardown foo-1") == 1
1335+
assert output.count("teardown foo-2") == 1
1336+
13111337
def test_funcarg_parametrized_and_used_twice(self, pytester: Pytester) -> None:
13121338
pytester.makepyfile(
13131339
"""

0 commit comments

Comments
 (0)