parameterized
parameterized copied to clipboard
Use unittest's subTest on py > 3.4?
Since 3.4, unittest supports subtests: https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
I think these could be used to let parameterized work without expand on unittest. E.g. a simplified version of the decorating wrapper could do something like:
for kwargs in params:
with self.subTest(**kwargs):
original_func(**kwargs)
(this is just for kwargs, for args some additionaly effort is needed to get parameter names through reflection).
The advantage of this would be that the test name does not change, so it is easier to run specific testcases (though I think there is no way to run just a single subTest).
This would be awesome! I don't have the cycles for a fix now, but I'd be more than happy to review a PR!
This would be awesome! I don't have the cycles for a fix now, but I'd be more than happy to review a PR!
I'm afraid I'm in the same position, but maybe after I crunch to a few more deadlines later :-)
It would be really interesting indeed. Up to now, I still can't execute a single test case from command line when using parameterized.
However, it also happens (very often in my projects) that I have both parameterized and subTests. I don't know how and if it's feasible to have nested subtests or, instead, expand the original ones.
A word of warning on this, using pytest to run unittests does not yet support unittest.subTest, any implementation would need to differentiate between using pytest and python -m unittest. unittest.subTest is lovely, so it would be great if parameterized and pytest could support it.
@wolever One problem with using subTest is that rather than each generated test running in its own instance of the test class with the setUp, test_method, tearDown sequence, they will all run in one instance with setUp, test_case1, ..., test_caseN, tearDown sequence. Is this acceptable? I suppose we can insert calls for setUp and tearDown, but the class instance is still the same, so it can accumulate state.
subTests have a big drawback - they don't interoperate with pytest and they display only failed tests. That's why I have migrated from .subTest to this lib.
I'm also not a fan of subTest, for the noted reasons (you can't select subtests, they accumulate state, and you can't reorder them). I would prefer that parameterized did not migrate to use them.