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

request.getfixturevalue('somevalue') returns lazyvalue #143

Closed
TheCaffinatedDeveloper opened this issue Nov 1, 2020 · 6 comments
Closed

Comments

@TheCaffinatedDeveloper
Copy link

I often use a pattern of performing some additional setup via another fixture by using the "special" request object to introspect the current test method. Once scenario involves using the request.getfixturevalue('foo') which should return the actual value being used in the current context but with pytest_cases I get the lazy object and have to call the .get() to get the value.

The problem is that I am not always using pytest_cases and would like my fixtures to be common and reused where it might not be a lazy object and thus not have a .get method. Perhaps I am doing something wrong, guidance requested please.

@smarie
Copy link
Owner

smarie commented Nov 2, 2020

Hi @TheCaffinatedDeveloper ! The principle of pytest_cases is that you create many case functions, that are used to parametrize your test function. Since case functions can take some time to execute and be memory consuming, I chose to follow the pattern that pytest uses everywhere else, that is, to execute these functions just when they are needed - exactly what happens with function-scope fixtures. This is why I use lazy_value to have them be called just before usage. See documentation for details.

One way to solve your issue is to use duck typing (try: val.get(); except AttributeError: pass) or to rely on is_lazy:

from pytest_cases.common_pytest_lazy_values import is_lazy

val = request.getfixturevalue('foo')
if is_lazy(val):
    val = val.get()

Note that I consider adding is_lazy to the official API (so that you can directly import from pytest_cases), do you think that this could be a good idea ?

@smarie
Copy link
Owner

smarie commented Nov 2, 2020

I have an afterthought here: currently _LazyValue does not have a cache mechanism as opposed to LazyTuple/_LazyTupleItem. So your get() call would trigger an additional call to the case function, instead of just calling it earlier.
I could quite easily add such a cache in a next version, following what I did in LazyTuple.

@TheCaffinatedDeveloper
Copy link
Author

@smarie Thank you for your timely response and suggestions. I was thinking of checking for the lazy items adhoc but was unaware of the is_lazy method - thank you for pointing me in that direction. I think it is worthwhile adding it to the official api. The current project I work on uses the test method doc string to update test descriptions (that get synced to a cloud requirements tool) during a "setup" phase and I am now parametrizing this doc_string using some of the values from the parameters being passed in via a fixture. Others may be doing the same or want to which is why I think adding it to the api would be worthwhile :)

PS. I hope pytest-cases gets incorporated into pytest core and you become rich and famous :)

@TheCaffinatedDeveloper
Copy link
Author

I have an afterthought here: currently _LazyValue does not have a cache mechanism as opposed to LazyTuple/_LazyTupleItem. So your get() call would trigger an additional call to the case function, instead of just calling it earlier.
I could quite easily add such a cache in a next version, following what I did in LazyTuple.

Great point, this would stay in line with only needing to call the TC function once and the second call would simply retrieve the value from cache - nice!

@smarie
Copy link
Owner

smarie commented Nov 9, 2020

Cool @TheCaffinatedDeveloper thanks for the feedback. I'll do my best when I have some bandwidth again - for now I am in paternity leave so my dev time is fairly limited as you can imagine

I hope pytest-cases gets incorporated into pytest core and you become rich and famous :)

:D if open source development was a path to fame and money we would all know it by now :) but I appreciate your kind words and positive feedback, this is a far better reward !

As far as pytest core is concerned, I think that this has been identified by the core team : pytest-dev/pytest#349 (comment)

@smarie smarie closed this as completed in edf52fa Nov 26, 2020
@smarie
Copy link
Owner

smarie commented Nov 26, 2020

@TheCaffinatedDeveloper this will ship with 2.4.0 probably topday when travis wakes up : it seems to be extremely slow these days

smarie pushed a commit that referenced this issue Dec 2, 2020
…gins can access the value without triggering an extra function call, but a new call is triggered for each pytest node, so as to prevent mutable object leakage across tests. Fixed #149 while ensuring no regression for #143.
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