Closed lawschlosser closed 11 months ago
What you want to is not possible with pytest in general. Pytest test execution is split into several phases. First is the collection phase in which fixture and test functions are found., followed by the call phase where the fixtures and test are executed. Parametrization happens during the collection phase. This means all parameters must be known before any fixture is executed! There was a similar question at the pytest repo recently, https://github.com/pytest-dev/pytest/discussions/11359, maybe this can help you.
Regarding usefixture
: This adds a mark on the function but pytest never sees this function in any context and so is not aware of the mark.
@jgersti ah, bummer. that explains some things. Thanks for looking into this, I appreciate your help!
Indeed this is not possible with pytest
. See also this https://github.com/smarie/python-pytest-cases/issues/235
Thanks @jgersti for providing the answer !
I would like to be able to parameterize a test/function whose cases are generated by a class. The class would be able to use fixtures during the case-generation process (i.e. collection stage).
In the docs there is this example of a case generator (abbreviated):
This works, but I'd like:
who
values('you', 'there')
to come from a callable (rather than hard-coded in the decorator).Below, I've added two superficial fixtures and a
my_callable
function to generate values for thewho
parameter.This fails with:
Ok, so it looks like
@parametrize
doesn't want to be given a callable; It wants to be given a resolved list of values. No problem, I can just callmy_callable
, and pass the results to the@parametrize
decorator, e.g.That results in this error:
...which I guess makes sense, but now I'm not sure how to proceed. I'm sure you've documented this (or a better/simpler approach) somewhere, so my apologies ahead of time. Any guidance/links would be much appreciated! Thank you!
Bonus puzzle
Continuing from that last attempt/failure, rather than augmenting the signature of
my_callable
(to indicate the need offixture1
andfixture2
), I figured maybe I could use@pytest.mark.usefixtures
instead. And you know what, this actually "worked" !!...except that it never actually ran/called the fixtures :frowning_face:
I would have thought there would be an error or warning if those fixtures were not run (since clearly I don't know what I'm doing), rather than silently failing (and in this case, passing)
Thanks so much for the work you've done!