Skip to content

Commit

Permalink
Add and adapt tests
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Tobias Deiminger committed Dec 16, 2021
1 parent 57ea297 commit a00c9b1
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions testing/example_scripts/issue_519.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ def checked_order():
assert order == [
("issue_519.py", "fix1", "arg1v1"),
("test_one[arg1v1-arg2v1]", "fix2", "arg2v1"),
("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"),
("test_one[arg1v1-arg2v2]", "fix2", "arg2v2"),
("test_two[arg1v1-arg2v1]", "fix2", "arg2v1"),
("test_two[arg1v1-arg2v2]", "fix2", "arg2v2"),
("issue_519.py", "fix1", "arg1v2"),
("test_one[arg1v2-arg2v1]", "fix2", "arg2v1"),
("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"),
("test_one[arg1v2-arg2v2]", "fix2", "arg2v2"),
("test_two[arg1v2-arg2v1]", "fix2", "arg2v1"),
("test_two[arg1v2-arg2v2]", "fix2", "arg2v2"),
]

Expand Down
26 changes: 26 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,32 @@ def test2(no_eq):
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*4 passed*"])

def test_optimize_by_reorder_indirect(self, pytester: Pytester) -> None:
"""Test reordering for minimal setup/teardown with indirectly parametrized fixtures. See #8914, #9350."""
pytester.makepyfile(
"""
import pytest
@pytest.fixture(scope="session")
def fix(request):
value = request.param["data"] if isinstance(request.param, dict) else request.param
print(f'prepare foo-%s' % value)
yield value
print(f'teardown foo-%s' % value)
@pytest.mark.parametrize("fix", [1, pytest.param({"data": 2}, id="2")], indirect=True)
def test1(fix):
pass
@pytest.mark.parametrize("fix", [pytest.param({"data": 2}, id="2"), 1], indirect=True)
def test2(fix):
pass
"""
)
result = pytester.runpytest("-s")
output = result.stdout.str()
assert output.count("prepare foo-1") == 1
assert output.count("prepare foo-2") == 1
assert output.count("teardown foo-1") == 1
assert output.count("teardown foo-2") == 1

def test_funcarg_parametrized_and_used_twice(self, pytester: Pytester) -> None:
pytester.makepyfile(
"""
Expand Down

0 comments on commit a00c9b1

Please sign in to comment.