Skip to content

Commit

Permalink
Add and adapt tests
Browse files Browse the repository at this point in the history
Add tests to assert pytest-dev#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
pytest-dev#9350 (review)
  • Loading branch information
Tobias Deiminger committed Jan 26, 2022
1 parent b891a92 commit 12559db
Show file tree
Hide file tree
Showing 2 changed files with 55 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
53 changes: 53 additions & 0 deletions testing/python/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,59 @@ def test2(no_eq):
result = pytester.runpytest()
result.stdout.fnmatch_lines(["*4 passed*"])

@pytest.mark.parametrize(
("parametrize1", "parametrize2"),
[
(
'"fix", [1, 2], indirect=True',
'"fix", [2, 1], indirect=True',
),
(
'"fix", [1, pytest.param({"data": 2}, id="2")], indirect=True',
'"fix", [pytest.param({"data": 2}, id="2"), 1], indirect=True',
),
(
'"fix", [{"data": 1}, {"data": 2}], indirect=True, ids=lambda d: MyEnum(d["data"])',
'"fix", [{"data": 2}, {"data": 1}], indirect=True, ids=lambda d: MyEnum(d["data"])',
),
(
'"fix", [{"data": 1}, {"data": 2}], indirect=True, ids=[1, "two"]',
'"fix", [{"data": 2}, {"data": 1}], indirect=True, ids=["two", 1]',
),
],
)
def test_reorder_and_cache(
self, pytester: Pytester, parametrize1, parametrize2
) -> None:
"""Test optimization for minimal setup/teardown with indirectly parametrized fixtures. See #8914, #9420."""
pytester.makepyfile(
f"""
import pytest
from enum import Enum
class MyEnum(Enum):
Id1 = 1
Id2 = 2
@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({parametrize1})
def test1(fix):
pass
@pytest.mark.parametrize({parametrize2})
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 12559db

Please sign in to comment.