-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Generate parametrize IDs for enum/re/class objects. #923
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
Conversation
Should I add a |
@@ -1,6 +1,9 @@ | |||
2.8.0.dev (compared to 2.7.X) | |||
----------------------------- | |||
|
|||
- parametrize now also generates meaningful test IDs for enum, regex and class | |||
objects. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe add something like "(as opposed to class instances)" just to be clear
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add a "Thanks Florian Bruhin for the PR" for consistency with the other entries. 😉
personally i'd escape the regex pattern and add can you move type(re.compile('')) to a constant? |
oh, and perhaps the module also, it's thinkable that people use different classes with the same module local name as parameter (i have at least one project where i do) |
In the same parametrized test? I'm not sure, I feel this is a rare occurrence and it would make the output of all the other cases more verbose without much gain... |
What do you mean?
Would you expect
Sure, will do.
I don't think those are very useful - I'd prefer to keep the test IDs short. I usually know a test is parametrized with classes (which I usually call So if you don't feel very strong about those two, I'd like to keep it as-is. |
Guys, This PR brought to my mind a topic which I have thought about before but forgot to bring up (actually I think it was @fogo which thought about this): what do you think about introducing a new hook which can be used by plugins to customize the ids generated by parametrize? This would allow users to generate readable parameter ids even for project-specific objects. Something along the lines of: def pytest_make_parametrize_id(config, val):
"""Return a user-friendly string representation of the given ``val`` that will be used
by @pytest.mark.parametrize calls. Return None if the hook doesn't know about ``val``.
""" (I'm not proposing to add this to the PR, just thought I would mention it here; the PR review/merge process should continue regardless of this). |
I like the idea! That might make sense for some Qt types for I wonder however: What's the reason pytest doesn't just use |
some types have inconsistent repr/str per run, dicts for example - the randomized hash-seed makes the problem more apparent that would create trouble for xdist |
I did the suggested style changes. (disclaimer: I didn't run any tests locally since I'm running out of battery) |
@The-Compiler, while you are it, please also move the try/except |
import enum | ||
except ImportError: | ||
# Only available in Python 3.4+ | ||
enum = None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can put a #pragma: no cover
comment on this line to keep code coverage happy here. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also note that there is a backport, so the comment is inaccurate :)
@nicoddemus I did that too, adjusted the comment and added a |
Thanks! 😄 Looks 👍 to me! If nobody has anything else to add, I will merge this later! |
@nicoddemus, re your hook idea, ids can be specified as a list of strings or as a callable (#351). I feel like that is probably equivalent to having a hook, not sure though. @The-Compiler, why doesn't pytest use str/repr on all objects, many dicts would quickly become unwieldy if printed like that, and many user-written str/reprs are actually not that good, for example would not be unique for different objects. Or again, would just be unwieldly. You want something that will fit on a line and preferably not take the whole line (IMO). |
It's not quite the same thing, as it requires that every It is of course a matter of convenience. Anyway, I will create a new issue with this and we can move the discussion to there. 😄 |
I agree - but the current handling already allows strings, where this might not be the case:
I wonder if pytest actually should do something like my |
Keeping in mind that the id should be unique as it forms the test name, I would say messing with string contents for ids is a bad idea. If the user has given distinct input we should try to avoid forcing them to specify ids, better to have boring ids. Yes if you have long strings you will have long IDs, but I don't see that as an argument for extending the badness to dicts and other types. Surely more a good case for using custom ids instead. |
basically for all complex datasets, custom id's win flat over printing directly, since meaningfull names |
Generate parametrize IDs for enum/re/class objects.
I noticed my tests were generating default IDs for those, so I thought why not make pytest a bit smarter 😉