Open cb109 opened 6 years ago
Hi, @cb109! For now it's not possible to expand lazy fixture inside of nested objects. Here is a similar issue.
I will not close it, because it's an issue anyway. Thanks!
I'd love to see this supported as well. We have a case where we want to add some metadata about existing fixtures when they are used as part of a different fixture. The metadata doesn't make sense to include in the original fixtures because it is only relevant in the new context. The natural way to tie the original fixtures and related metadata together would be in the params of the new fixture.
Another place where this shows up is parametrized tests which operate over arbitrary numbers of arguments, which is something I often use.
Toy example:
@pytest.mark.parametrize(
'args',
(
(),
(None, None, None),
(lazy_fixture('some_fixture'), 12.5, 'foo', ['hello world']),
),
)
def test_with_arbitrary_list_of_args(args):
# Do something with args here...
Here on the third run of the test, args[0]
will be <LazyFixture "some_fixture">
rather than the result of that fixture; to get the actual result I am forced to use a fixed number of explicitly-named arguments, it seems.
I also have this issue. My use case is a single test that checks that all the methods that should raise a certain exception do, so in my parametrize I pass in the Callable as one argument and the "extra parameters" dictionary as the second. I then expand this to pass them in as keyword args to the Callable, but one of these extra parameters, for one method only, needs to use a fixture (as the exception is only thrown when all other params are valid).
Count me also among those who consider this issue important.
I tackled it as well, in my case I wanted to pass a dictionary with multiple fixtures. Since it was passed as a sequence I don't have the access to the fixture value within. +1
for this request.
This feature would be useful. But since there is no plans to release this, I am moving to pytest-cases
.
Finding this important as well!
My case, inside a dictionary:
@pytest.mark.parametrize(
argnames=["args", "error"],
argvalues=[
({"filename": "1000"}, TypeError),
({"filename": pytest.lazy_fixture("filename"), "threshold": "threshold"}, TypeError),
({"filename": pytest.lazy_fixture("no_filename")}, FileNotFoundError),
({"filename": pytest.lazy_fixture("filename"), "threshold": -10}, ValueError),
({"filename": pytest.lazy_fixture("filename"), "threshold": 1}, ValueError),
({"filename": pytest.lazy_fixture("filename"), "threshold": 10}, ValueError),
]
)
def test_ibd_errors( # noqa: D103
otter_qc: OtterQC, args: dict[str, Any], error: Exception
):
with pytest.raises(error):
otter_qc.ibd(**args)
My use case requires preparing lists of things instead of just single things one by one, please see your example adapted accordingly below.
Inside
test_func()
thesome_list
will be something like[<LazyFixture "one">, <LazyFixture "two">]
where I would have expected that this had been expanded/resolved to[1, 2]
.Is there a way to always expand /resolve in a test context no matter how wrapped or nested the
LazyFixture
is?Cheers