[Python-ideas] Catching of multiple exceptions without halting execution
Nick Coghlan
ncoghlan at gmail.com
Tue Jun 25 02:39:15 CEST 2013
On 25 June 2013 08:46, Nick Coghlan <ncoghlan at gmail.com> wrote:
> Python 3.4 adds subtest support to the unittest module, allowing data driven
> test cases to easily capture exceptions as failures before moving on to
> check other inputs. The feature can also be used to capture the results of
> multiple assertions.
Specifically, the subtest support is based on a new test case method
that returns a context manager and looks like this:
>>> import unittest
>>> class SubTestExample(unittest.TestCase):
... def test_values_even(self):
... values = range(6)
... for i in values:
... with self.subTest(i=i):
... self.assertEqual(i % 2, 0)
...
>>> unittest.main()
======================================================================
FAIL: test_values_even (__main__.SubTestExample) (i=1)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 6, in test_values_even
AssertionError: 1 != 0
======================================================================
FAIL: test_values_even (__main__.SubTestExample) (i=3)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 6, in test_values_even
AssertionError: 1 != 0
======================================================================
FAIL: test_values_even (__main__.SubTestExample) (i=5)
----------------------------------------------------------------------
Traceback (most recent call last):
File "<stdin>", line 6, in test_values_even
AssertionError: 1 != 0
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=3)
See http://docs.python.org/dev/library/unittest#distinguishing-test-iterations-using-subtests
for more details.
The first 3.4 alpha is scheduled for August, with the final release
anticipated in February next year (see
http://www.python.org/dev/peps/pep-0429/)
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list