07.09.21 05:31, Leonardo Freua пише:
> When writing some unit tests with the standard Unittest library, I missed 
> being able to create parameterized tests. This functionality exists in PyTest 
> (https://docs.pytest.org/en/6.2.x/parametrize.html) and there is also a 
> library called *parameterized*(https://github.com/wolever/parameterized) 
> which aims to add this functionality.
> 
> However, I think it would be beneficial to have a decorator in Python's 
> standard Unittest library.
> 
> Note: If this functionality exists natively in Python, please put some 
> example or documentation link below.

It was discussed before and subTest() was added as more general alternative.

Instead of

    @parametrize("a", [1, 2, 10])
    def test(self, a):
        ...

you should write

    def test(self):
        for a in [1, 2, 10]:
            with self.subTest(a=a):
                ...

The advantage is that you can generate parameters at run time. You can
add a code before and after the loop. Use several sequential loops.
There are also other use cases for subTest().

The disadvantage is that it adds at least two indentation levels (more
if you use nested loops) to the test code. A simple decorator would be
more convenient in simple cases.

It is easy to implement the decorator in terms of subtests, but
currently there are some issues with outputting results of subtests:
https://bugs.python.org/issue25894
https://bugs.python.org/issue30856

_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5ES2KP34Z4PC75MBAHAAL4JYRQJ7UDJ6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to