Skip to content
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

Using a @fixture returning a list of params to parametrize a test #235

Closed
DahanDv opened this issue Nov 7, 2021 · 3 comments
Closed

Using a @fixture returning a list of params to parametrize a test #235

DahanDv opened this issue Nov 7, 2021 · 3 comments

Comments

@DahanDv
Copy link

DahanDv commented Nov 7, 2021

Hey fellow maintainers, I'll be glad to have your guidance..

I'm trying to use pytest cases to parameterize a test with a list of tuples(str, iterable) that is yielded from a (pytest-cases)@fixture, but something isn't working for me.

It goes like the following:

# We have a native @pytest.fixture that yield a value
@pytest.fixture
def give_some_val():
    return val

# Then I use this val in a pytest-cases @fixture so I can use it to parameterized my test later
@fixture
def prepare_params(give_some_val):
    val_to_list_of_tuples = data_manipulation(give_some_val)
    yield val_to_list_of_tuples

To clarify, the yielded value looks something like:
[('event_name': {'uuid1','uuid2'}), ('another_event_name': {'uuid3', 'uuid4'}),(..),....]
again, list of tuples(string, set) .

when I try to implement this (either with @paytest.mark.parametrize or via @parametrize) like so:

@parametrize(argsnames="event,data", argvals="prepare_params"
def test(event, data, another_native_pytest_fixture):
      assert make_sense(data, another_native_pytest_fixture), f"{event} failed"`

it seems like pytest can't create more tests nodes..
instead, it creates just one test node and it fails to recognize the structure of ("name1,name2", list_of_tuples_with_2_elements)
instead it treats every element in the list as a single param.
I mean:
event = ('event_name': {'uuid1','uuid2'})
data = ('another_event_name': {'uuid3', 'uuid4'})

and that's it. no more test nodes.

I've gone through the docs multiple times but I can't find the answer.
Highly appreciate your time if you got so far!

@smarie
Copy link
Owner

smarie commented Nov 8, 2021

Hi @DavidDahan1, thanks for this issue ! I updated it because it was not readable "as is" ..
I'll have a look

@smarie
Copy link
Owner

smarie commented Nov 8, 2021

It seems that you want a fixture to return a list of parameters. This is not possible with pytest, by design. See this post that explains it in details : https://stackoverflow.com/a/56865893/7262247

See also this answer: #205 (comment)

Note that pytest-cases is not repsonsible for this limitation, it is the way fixtures and parameters are defined in pytest.

So you'll need to create a copy of give_some_val that is not a fixture, and call it in the middle of the pytest module in order to define the list in the parametrize.

val_to_list_of_tuples = data_manipulation(give_some_val_not_fixture)

@parametrize(argsnames="event,data", argvals=val_to_list_of_tuples)
# either a test, or a fixture, would be parametrized that way.

@smarie
Copy link
Owner

smarie commented Dec 15, 2021

Closing this now - please feel free to reopen if the above answer does not correspond to your actual question

@smarie smarie closed this as completed Dec 15, 2021
@smarie smarie pinned this issue Dec 15, 2021
@smarie smarie changed the title Help needed - using @fixture to parametrize a test Using a @fixture returning a list of parameters to parametrize a test Dec 15, 2021
@smarie smarie changed the title Using a @fixture returning a list of parameters to parametrize a test Using a @fixture returning a list of params to parametrize a test Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants