-
Notifications
You must be signed in to change notification settings - Fork 39
How to unpack fixtures from parametrize_with_cases? #201
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
Comments
Thanks for reporting @last-partizan and sorry for the delay in answering ! I'll investigate |
Indeed applying The workaround that you provide seems very fine to me, I think that you can simplify it further with unpacking directly into Also why do you have to recopy the http response test ? Does the test embedded in Returns200 or Returns404 not "see" the updated fixtures ? |
@smarie thanks for investigation :) Unpacking directly into I copied http resonse test just for clarity here. |
This is probably because my current implementation does not unpack them in the class but the module, and therefore the ones from the parent class take precedence. I need to investigate this |
I confirm: unpack into creates the corresponding fixtures in the module. I had a quick look whether I could fix it by using The only solution I can imagine would be to mark the created fixture with a special symbol, or to register it somewhere, and to do the unpacking later, probably in a pytest hook. This would require a good knowledge of pytest hooks to do it in an elegant way (doing it right when the module is collected would seem relevant so that the fixtures exist "fast", directly after the module is collected, and they can therefore be used normally by pytest in the subsequent phases. I have to admit that my current bandwidth is not compliant with such investigation. If you would be able to provide a working example of pytest hook where a fixture is late-constructed at collection phase without harm for the tests using it, I would be happy to integrate it in the right place to fix this ! Thanks again for spotting this @last-partizan |
Probably, such deep dive into pytest is not the right way to fix this, and i don't know pytest internals to do this. Maybe this could be solved some other way, like this.
I'll take a look at it later. |
Oh yes of course, this should do the trick ! But note that for the same reason, this would not be "clean": a and b would be defined in the class (because you assign the output to variables that are in the class), and also in the module (because Anyway, if it works then the workaround would still be better than nothing |
@last-partizan did it do the trick eventually ? |
@smarie no, it didn't. from pytest_cases import fixture, parametrize_with_cases, unpack_fixture
from pytest_drf import APIViewTest, Returns404, UsesPostMethod
class Cases:
def case_one(self):
return "/", {"param": "value"}
@fixture
@parametrize_with_cases("case", Cases)
def case(case):
return case
class TestAPIView(APIViewTest, UsesPostMethod, Returns404):
url, data = unpack_fixture("url, data", case) Results in
|
Ok thanks @last-partizan , the issue is the same as for the other : when I will add an |
Hi @last-partizan , there is a new import pytest
from pytest_cases import unpack_fixture, fixture
@fixture
@pytest.mark.parametrize("o", ['hello', 'world'])
def c(o):
return o, o[0]
class TestClass:
a, b = unpack_fixture("a,b", c, in_cls=True)
def test_function(self, a, b):
assert a[0] == b Can you please check that it fixes your issue ? (use version 3.6.0+) |
Yes, it works as expected. Thank you! |
Cool ! Thanks for the quick feedback @last-partizan |
I want to use pytest-cases with pytest-drf, which makes testing DRF looks like this:
but, actual check
test_it_returns_200
is inside Returns200 class, and i have no way to parametrize it with parametrize_with_cases.Well, i can do this:
but this is too much boilerplate. Any ideas how to simplify this?
Ideal variant would be something like this:
but it throws error
ValueError: parameter 'url' not found in test function signature 'TestView()'
The text was updated successfully, but these errors were encountered: