This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Subtests (unittest)
Type: enhancement Stage: resolved
Components: Library (Lib) Versions: Python 3.7
process
Status: closed Resolution: out of date
Dependencies: Superseder:
Assigned To: Nosy List: porton, r.david.murray
Priority: normal Keywords:

Created on 2018-08-10 17:56 by porton, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (2)
msg323375 - (view) Author: Victor Porton (porton) Date: 2018-08-10 17:56
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.
msg323376 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-08-10 17:59
https://docs.python.org/3/library/unittest.html#distinguishing-test-iterations-using-subtests
History
Date User Action Args
2022-04-11 14:59:04adminsetgithub: 78556
2018-08-10 17:59:14r.david.murraysetstatus: open -> closed

nosy: + r.david.murray
messages: + msg323376

resolution: out of date
stage: resolved
2018-08-10 17:56:26portoncreate