-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
Added helper to test.support for parameterizing tests #135120
Copy link
Copy link
Open
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixespendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtestsTests in the Lib/test dirTests in the Lib/test dirtype-featureA feature request or enhancementA feature request or enhancement
Metadata
Metadata
Assignees
Labels
3.13bugs and security fixesbugs and security fixes3.14bugs and security fixesbugs and security fixes3.15pre-release feature fixes, bugs and security fixespre-release feature fixes, bugs and security fixespendingThe issue will be closed if no feedback is providedThe issue will be closed if no feedback is providedtestsTests in the Lib/test dirTests in the Lib/test dirtype-featureA feature request or enhancementA feature request or enhancement
This is a simple decorator modeled after
pytest.mark.parametrize. It allows to "parametrize" tests, each parametrized test is run as a subtest.The decorator specifies the name(s) of parameters (as an iterable or a comma separated string) and their values (an iterable of values for parameters):
is equivalent to
It allows to save 2 levels of indentation.
Multiple decorators can be used, this generates a Descartes production of parameter values:
is equivalent to
It allows to save even more 2 levels of indentation.
It has also optional keyword-only parameter
_do_cleanups._do_cleanups=Truetells to calldoCleanups()after each subtest. This is temporary feature, only for use in test_ntpath. In general, it is wrong to do this becausedoCleanups()will also calls callbacks added insetUp. It will be replaced either byaddSubTestCleanup()(see https://discuss.python.org/t/unittest-add-addcleanup-to-subtest/91827 and #134079) or by other methods (for example by adding fences for cleanup). Also, it will not be needed for real test parameterization.There are differences from "real" parameterization:
setUpandtearDownare only run once, before and after all subtests.I plan to implement real parameterization, this is why I only propose to add that decorator in
test.support, not inunittest. In any case it would be backported intest.support. We may finally add both decorators inunittestif this does not create confusion.The name
subTestsis intentionally chosen to not confuse withparametrizewhich is reserved for real test parameterization.The PR contains also several examples of using that decorator.
Linked PRs