parameterized icon indicating copy to clipboard operation
parameterized copied to clipboard

Use unittest's subTest on py > 3.4?

Open matthijskooijman opened this issue 5 years ago • 7 comments

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).

matthijskooijman avatar Apr 12 '20 18:04 matthijskooijman

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!

wolever avatar Apr 12 '20 21:04 wolever

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 :-)

matthijskooijman avatar Apr 13 '20 09:04 matthijskooijman

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.

tigerjack avatar Jun 18 '20 13:06 tigerjack

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.

worc3131 avatar Feb 15 '21 09:02 worc3131

@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.

eltoder avatar Mar 22 '21 16:03 eltoder

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.

KOLANICH avatar Dec 21 '22 15:12 KOLANICH

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.

adamchainz avatar Mar 17 '23 15:03 adamchainz