-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 marker keyword arguments in marker expression (test selection via -m
)
#12281
Comments
The feature request makes sense to me, I think it would be great to allow matching on mark arguments (there are two kinds -- positional and keyword arguments), and something like the function call syntax seems natural for this. For the match expression grammar, it shouldn't pose a big challenge to add. We would need to decide whether the argument values (the The bigger implementation issue would be the evaluation of the expression. Currently, to optimize the evaluation, we compile the expression to a python AST, then |
If we are to implement this, I propose we keep it simple, as it would be possible to also write |
I can see the appeal of this, and I agree in theory it makes sense to keep things close to a Python-like syntax. However, at the same time, I'm also worried this might be a bottomless pit. Where do we stop in terms of types of arguments we support? Allowing However, what about, say, enums? Surely there's an argument to be made for enums, as in Maybe though, we could limit this to |
Running into this thought myself, I was wondering if we could instead consider a bit of a tangent to avoid pitfalls mentioned by @The-Compiler. How about introducing a new interface called It would look like: @pytest.mark.annotated("annotation1", "annotation2", ...)
def test_my_function(...):
pass The selection can be with a slightly specialized syntax:
which can be combined with other non-annotated markers if needed: pytest -m "awesome and [annotation1] and [annotation2]" This is surely limiting, but maybe that's not a bad thing. For my use case, this would help with adding bug number/jira ticket annotations to test cases. |
What's the problem this feature will solve?
It's possible to create custom markers like so:
@pytest.mark.my_device_under_test
This marker can the be used to de/select tests via marker expressions, e.g.:
pytest -m "not my_device_under_test"
It is also possible to define keyword arguments for custom markers, e.g.:
@pytest.mark.my_device_under_test(with_serial="1234")
Now, it would be amazing to select tests further depending on their keyword arguments + values, e.g.:
pytest -m "my_device_under_test(with_serial="1234)"
So only tests with that specific marker + that specific keyword argument+value would be run.
Describe the solution you'd like
Marker keyword argument selection in pytest's marker expression.
Alternative Solutions
There was a
nose
test plugin, which supported something similar here. This could be ported to pytest but comes with it's ownattr
decorator.A pytest plugin could probably do this in an extra step as well, but would somehow need to disable pytest's internal marker selection plugin here as it would collide/fail otherwise.
Additional context
When skimming through the code I saw that @bluetech implemented a custom parsers some years ago. I'm not very familiar with the topic, as in how much effort it would be support such feature. I'd be interested to contribute though.
The text was updated successfully, but these errors were encountered: