-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support generator cases #229
Comments
Seems like some fun and something I was looking for before, any pointers? |
A case function is "just" a Below you can find info on lazy values, but the more I think about it, the more I think that the simple way is to detect generator cases and force-turn them into fixtures (the same way as for parametrized case functions or case functions requiring fixtures). Much simpler and requires no pytest hack since fixtures suport this. -- Old post, probably bad idea There is an example in the API reference : https://smarie.github.io/python-pytest-cases/pytest_goodies/#parametrize From what I remember there is a I have no clue on where and when (which pytest step) we should interact with lazy values to have this setup/teardown thing. Maybe this is a bad idea ? |
This is done in
You see that there is one if branch that creates |
Hi @smarie, I'll take a look throughout this week, thanks for the detailed write up :) |
Thank you guys for an interesting package! I think I came across a problem that aligns with this discussion. Assume a test file import typing
import pytest_cases
def case_one() -> typing.Iterator[int]:
yield 1
def case_two() -> typing.Iterator[int]:
yield 2
@pytest_cases.parametrize_with_cases("number", cases=".")
def test_number(number: int) -> None:
assert isinstance(number, int) This fails with
I would have expected that the value passed into the test is an However, I think this becomes more interesting if the test case produces more than one value, e.g. def case_numbers() -> typing.Iterator[int]:
yield from range(10) Curious to get your thoughts on this! |
Hi @jenstroeger thanks for the kind words ! Sorry for answering very late. Yes today the case functions are normal functions, therefore if you provide a generator (a function containing a yield statement) then pytest-cases will not handle it particularly well. This is because not all case functions are turned into pytest fixtures today. The discussion above concerns supporting yield statements in case functions, BUT ONLY ONE (like pytest fixtures). This can be done by forcing the conversion of case functions containing yield to a fixture, following the writeup in #229 (comment) Supporting case functions with multiple yield statements (as you suggest) was, ironically, supported in the very first versions of pytest-cases, with the
Therefore I agree that this could be a good idea. Still, how to make this elegant while generic ? There are implementation details to handle, that can make things very complex.
If you are still willing to discuss all of this :) I suggest that you recopy this post into a dedicated new issue so that we take the discussion there. Thanks ! |
Currently if we wish to perform some setup/teardown activity on a particular case, we have to create a distinct fixture to do so and reference it in the case.
Generator case functions could directly be supported out of the box instead.
The text was updated successfully, but these errors were encountered: