[issue7897] Support parametrized tests in unittest

Andre Herbst report at bugs.python.org
Fri Sep 20 15:18:32 EDT 2019


Andre Herbst <moormaster at gmx.net> added the comment:

+1 for the feature

Subtests make the test results of all asserts visible at test execution time but decrease the readability of a test:

@parameterized([2,4,6])
def test_method_whenCalled_returnsNone(self, a):
    # 1) arrange
    something = Something()

    # 2) act
    result = something.method(a)

    # 3) assert
    self.assertIsNone(result)

When using subtests the phases of 1) arrange, 2) act, 3) assert are not clearly separated, the unit test contains logic and two additional indentation levels that could be avoided with parameterized tests:

def test_method_whenCalled_returnsNone(self, a):
    # 1) arrange
    something = Something()

    for a in [2,4,6]:
        with self.subTest(a=a):
            # 2) act
            result = something.method(a)

            # 3) assert
            self.assertIsNone(result)

----------
nosy: +moormaster

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue7897>
_______________________________________


More information about the Python-bugs-list mailing list