-
How do I pass objects in The code I used to generate is attached below. When I pass a string list in sample.py::TestTestObj::test_some_thing[obj] a--17973f51-280a-4ec2-b8d8-1599fdd41686
sample.py::TestTestObj::test_some_think[obj] a--0055fb8e-7d57-4b21-815e-60a0015c4821
sample.py::TestTestStr::test_some_thing[str] b--0a23f207-1e59-405f-aa6b-f16c22152a4b
sample.py::TestTestStr::test_some_think[str] b--0a23f207-1e59-405f-aa6b-f16c22152a4b As you see first and second line has different UUIDs (this was the list of objects) while the fixture has class scope, but 3 and 4 have the same UUID(this was the list of strings).
class MyClass:
def __init__(self, my_param: str):
self.my_param = my_param
import pytest
from my_class import MyClass
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
if "fixture_obj" in metafunc.fixturenames:
metafunc.parametrize("fixture_obj", [MyClass("a")], indirect=True, ids=["obj"])
if "fixture_str" in metafunc.fixturenames:
metafunc.parametrize("fixture_str", ["b"], indirect=True, ids=["str"])
import logging
import uuid
from typing import Any
import pytest
logger = logging.getLogger(__name__)
@pytest.fixture(scope="class")
def fixture_obj(request: Any):
return f"{request.param.my_param}--{str(uuid.uuid4())}"
@pytest.fixture(scope="class")
def fixture_str(request: Any):
return f"{request.param}--{str(uuid.uuid4())}"
class TestTestObj:
def test_some_thing(self, fixture_obj):
print(fixture_obj)
def test_some_think(self, fixture_obj):
print(fixture_obj)
class TestTestStr:
def test_some_thing(self, fixture_str):
print(fixture_str)
def test_some_think(self, fixture_str):
print(fixture_str) execute test as pytest -s --log-cli-level INFO sample.py |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
if "fixture_str" in metafunc.fixturenames:
values = [str(uuid.uuid4())]
metafunc.parametrize("fixture_str", values, indirect=True, ids=["str"]) Even fixtures parameterized like these fail! even though it's a list of strings |
Beta Was this translation helpful? Give feedback.
-
@RonnyPfannschmidt @nicoddemus Hey folks, sorry to bother you. Wanted some help with this issue if you are aware of it. |
Beta Was this translation helpful? Give feedback.
Thanks a lot, Ronny, this helped.