-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
gh-135120: Add test.support.subTests() #135121
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
base: main
Are you sure you want to change the base?
gh-135120: Add test.support.subTests() #135121
Conversation
6358e5a
to
24bbca2
Compare
24bbca2
to
8c0608a
Compare
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.
Thank you! This looks very useful :)
Lib/test/test_ntpath.py
Outdated
def _parameterize(*parameters): | ||
"""Simplistic decorator to parametrize a test | ||
|
||
Runs the decorated test multiple times in subTest, with a value from | ||
'parameters' passed as an extra positional argument. | ||
Calls doCleanups() after each run. | ||
|
||
Not for general use. Intended to avoid indenting for easier backports. | ||
|
||
See https://discuss.python.org/t/91827 for discussing generalizations. | ||
""" | ||
def _parametrize_decorator(func): | ||
def _parameterized(self, *args, **kwargs): | ||
for parameter in parameters: | ||
with self.subTest(parameter): | ||
func(self, *args, parameter, **kwargs) | ||
self.doCleanups() | ||
return _parameterized | ||
return _parametrize_decorator | ||
|
||
|
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.
For test_ntpath
and test_posixpath
, could you leave _parameterize
in for now, making it an adapter for support.subTests
?
This was added recently, in a security change that was backported everywhere and received limited review. I think it would be good to try keeping the tests stable (textually) for a while (a few months?), to make backports easier.
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.
Done.
Lib/test/support/__init__.py
Outdated
@functools.wraps(func) | ||
def wrapper(self, /, *args, **kwargs): | ||
for values in arg_values: | ||
if len(arg_names) == 1: |
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.
I'd change the logic to if the original arg_names was a string without a comma.
That is, I'd expect subTests(['number'], ([1], [2])
to set number
to 1
and 2
.
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.
Done.
Lib/test/support/__init__.py
Outdated
"""Run multiple subtests with different parameters. | ||
""" | ||
if isinstance(arg_names, str): | ||
arg_names = arg_names.split(',') |
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.
Could this also strip()
the names? We should allow putting space after a comma.
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.
Done. I initially used an idiom from namedtuple, but then decided that comma-only is enough for initial implementation in test.support
.
""" | ||
if isinstance(arg_names, str): | ||
arg_names = arg_names.split(',') | ||
def decorator(func): |
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.
With the current PR, if you use a generator for arg_values
, a second run of the test will silently do nothing ☠️
def decorator(func): | |
arg_values = tuple(arg_values) | |
def decorator(func): |
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.
Done.
16eeaf5
to
8b3ede1
Compare
Uh oh!
There was an error while loading. Please reload this page.