chore: fixture tests: migrate to f-strings

This commit is contained in:
Ronny Pfannschmidt 2024-02-21 16:18:49 +01:00
parent 96b786b63b
commit a6513d62c9
1 changed files with 19 additions and 23 deletions

View File

@ -2199,14 +2199,14 @@ class TestAutouseManagement:
) -> None: ) -> None:
"""#226""" """#226"""
pytester.makepyfile( pytester.makepyfile(
""" f"""
import pytest import pytest
values = [] values = []
@pytest.fixture(%(param1)s) @pytest.fixture({param1})
def arg1(request): def arg1(request):
request.addfinalizer(lambda: values.append("fin1")) request.addfinalizer(lambda: values.append("fin1"))
values.append("new1") values.append("new1")
@pytest.fixture(%(param2)s) @pytest.fixture({param2})
def arg2(request, arg1): def arg2(request, arg1):
request.addfinalizer(lambda: values.append("fin2")) request.addfinalizer(lambda: values.append("fin2"))
values.append("new2") values.append("new2")
@ -2215,8 +2215,7 @@ class TestAutouseManagement:
pass pass
def test_check(): def test_check():
assert values == ["new1", "new2", "fin2", "fin1"] assert values == ["new1", "new2", "fin2", "fin1"]
""" # noqa: UP031 (python syntax issues) """
% locals()
) )
reprec = pytester.inline_run("-s") reprec = pytester.inline_run("-s")
reprec.assertoutcome(passed=2) reprec.assertoutcome(passed=2)
@ -3096,21 +3095,21 @@ class TestFixtureMarker:
) -> None: ) -> None:
"""#246""" """#246"""
pytester.makepyfile( pytester.makepyfile(
""" f"""
import pytest import pytest
values = [] values = []
@pytest.fixture(scope=%(scope)r, params=["1"]) @pytest.fixture(scope={scope!r}, params=["1"])
def fix1(request): def fix1(request):
return request.param return request.param
@pytest.fixture(scope=%(scope)r) @pytest.fixture(scope={scope!r})
def fix2(request, base): def fix2(request, base):
def cleanup_fix2(): def cleanup_fix2():
assert not values, "base should not have been finalized" assert not values, "base should not have been finalized"
request.addfinalizer(cleanup_fix2) request.addfinalizer(cleanup_fix2)
@pytest.fixture(scope=%(scope)r) @pytest.fixture(scope={scope!r})
def base(request, fix1): def base(request, fix1):
def cleanup_base(): def cleanup_base():
values.append("fin_base") values.append("fin_base")
@ -3123,8 +3122,7 @@ class TestFixtureMarker:
pass pass
def test_other(): def test_other():
pass pass
""" # noqa: UP031 (python syntax issues) """
% {"scope": scope}
) )
reprec = pytester.inline_run("-lvs") reprec = pytester.inline_run("-lvs")
reprec.assertoutcome(passed=3) reprec.assertoutcome(passed=3)
@ -3310,42 +3308,40 @@ class TestRequestScopeAccess:
def test_setup(self, pytester: Pytester, scope, ok, error) -> None: def test_setup(self, pytester: Pytester, scope, ok, error) -> None:
pytester.makepyfile( pytester.makepyfile(
""" f"""
import pytest import pytest
@pytest.fixture(scope=%r, autouse=True) @pytest.fixture(scope={scope!r}, autouse=True)
def myscoped(request): def myscoped(request):
for x in %r: for x in {ok.split()}:
assert hasattr(request, x) assert hasattr(request, x)
for x in %r: for x in {error.split()}:
pytest.raises(AttributeError, lambda: pytest.raises(AttributeError, lambda:
getattr(request, x)) getattr(request, x))
assert request.session assert request.session
assert request.config assert request.config
def test_func(): def test_func():
pass pass
""" # noqa: UP031 (python syntax issues) """
% (scope, ok.split(), error.split())
) )
reprec = pytester.inline_run("-l") reprec = pytester.inline_run("-l")
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)
def test_funcarg(self, pytester: Pytester, scope, ok, error) -> None: def test_funcarg(self, pytester: Pytester, scope, ok, error) -> None:
pytester.makepyfile( pytester.makepyfile(
""" f"""
import pytest import pytest
@pytest.fixture(scope=%r) @pytest.fixture(scope={scope!r})
def arg(request): def arg(request):
for x in %r: for x in {ok.split()!r}:
assert hasattr(request, x) assert hasattr(request, x)
for x in %r: for x in {error.split()!r}:
pytest.raises(AttributeError, lambda: pytest.raises(AttributeError, lambda:
getattr(request, x)) getattr(request, x))
assert request.session assert request.session
assert request.config assert request.config
def test_func(arg): def test_func(arg):
pass pass
""" # noqa: UP031 (python syntax issues) """
% (scope, ok.split(), error.split())
) )
reprec = pytester.inline_run() reprec = pytester.inline_run()
reprec.assertoutcome(passed=1) reprec.assertoutcome(passed=1)