Replies: 5 comments
-
| Due to the removal of relevant context all i can say with the code provided is make the parameter a own fixture and stop missing with the internals | 
Beta Was this translation helpful? Give feedback.
-
| What context is missing? | 
Beta Was this translation helpful? Give feedback.
-
| from dataclasses import dataclass
import pytest
@dataclass
class DataShape:
    param1: int
    param2: str
@pytest.fixture
def common_params(request: pytest.FixtureRequest) -> DataShape:
    return request.param
@pytest.fixture
def param1(common_params: DataShape) -> int:
    return common_params.param1
@pytest.fixture
def param2(common_params: DataShape) -> str:
    return common_params.param2
@pytest.fixture
def stash(param2: str) -> str:
    return param2
@pytest.mark.parametrize(
    'common_params',
    (DataShape(42, 'криївка'), ),
    indirect=True,
)
@pytest.mark.usefixtures('common_params')
def test_accessing_param2(stash: str) -> None:
    assert stash == 'криївка' | 
Beta Was this translation helpful? Give feedback.
-
| Suppose  import pytest
def flaky_computation1(param1):
    return param1 + 1
def flaky_computation2(param2):
    return param2 + "!"
class DataShape:
    def __init__(self, param1: int, param2: str):
        self.param1 = flaky_computation1(param1)
        self.param2 = flaky_computation2(param2)
@pytest.fixture
def common_params(request: pytest.FixtureRequest) -> DataShape:
    return request.param
@pytest.fixture
def param1(common_params: DataShape) -> int:
    return common_params.param1
@pytest.fixture
def param2(common_params: DataShape) -> str:
    return common_params.param2
@pytest.fixture
def stash(param2: str) -> str:
    return param2
@pytest.mark.parametrize(
    'common_params',
    (DataShape(42, 'криївка'), ),  # shouldn't call flaky_computation1()
    indirect=True,
)
@pytest.mark.usefixtures('common_params')
def test_accessing_param2(stash: str) -> None:
    assert stash == 'криївка!'
@pytest.mark.parametrize(
    'common_params',
    (DataShape(42, 'криївка'), ),  # should be reused from the parameter of test_accessing_param2
    indirect=True,
)
def test_full(common_params) -> None:
    assert str(common_params.param1) + common_params.param2 == "43криївка!" | 
Beta Was this translation helpful? Give feedback.
-
| Converting this into a discussion because I don't see anything actionable for pytest here. | 
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Downloading images from the Internet is flaky. I'm trying to reuse the images in tests to reduce instability. I also want the images to be removed from memory after they aren't needed. I'm trying to achieve that with the following code
test_images[images2]printstest_images: images[image-1 image-1 image-1]instead of 1, 2, 3. I'd likerequest.getfixturevalue()to have a parameter to be passed to the fixture. I've checkedpytest-lazy-fixturebut its fixtures cannot be parametrized (https://stackoverflow.com/a/62166129/25242963).Beta Was this translation helpful? Give feedback.
All reactions