[New-bugs-announce] [issue34375] Subtests (unittest)

Victor Porton report at bugs.python.org
Fri Aug 10 13:56:26 EDT 2018


New submission from Victor Porton <porton at narod.ru>:

The following is a fragment of a real code:

~~~
    def test_run_xinlude(self):
        # stub_stdin(self, Global.get_resource_bytes("tests/core/data/xml/xinclude.xml"))
        for next_script_mode in ['doc1', 'doc2']:
            for order in ['breadth', 'depth']:
                with capture_stdin_and_stdout():
                    command_line.main(['-r',
                                       order,
                                       'chain',
                                       Global.get_filename("tests/core/data/xml/xinclude.xml"),
                                       '-u',
                                       'http://portonvictor.org/ns/trans/precedence-include',
                                       '-s',
                                       next_script_mode])
                    self.assertEqual(sys.stdout.buffer.getvalue(), TestUtility.XInclude_output,
                                     "for next_script=%s, order=%s" % (next_script_mode, order))
~~~

I wrote it in one test method instead of four similar methods.

It has the deficiency that if the first test fails, the three remaining tests are skipped.

I propose to add `subtest` context manager to use it like:

~~~
            with subtest():
                with capture_stdin_and_stdout():
                    command_line.main(['-r',
                                       order,
                                       'chain',
                                       Global.get_filename("tests/core/data/xml/xinclude.xml"),
                                       '-u',
                                       'http://portonvictor.org/ns/trans/precedence-include',
                                       '-s',
                                       next_script_mode])
                    self.assertEqual(sys.stdout.buffer.getvalue(), TestUtility.XInclude_output,
                                     "for next_script=%s, order=%s" % (next_script_mode, order))
~~~

which would split our test into four independent tests.

----------
components: Library (Lib)
messages: 323375
nosy: porton
priority: normal
severity: normal
status: open
title: Subtests (unittest)
type: enhancement
versions: Python 3.7

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


More information about the New-bugs-announce mailing list