[New-bugs-announce] [issue25651] Confusing output for TestCase.subTest(0)

Ezio Melotti report at bugs.python.org
Tue Nov 17 21:34:13 EST 2015


New submission from Ezio Melotti:

When a single positional argument is passed to subTest(), if the argument is false, its value won't be displayed in the output -- (<subtest>) will appear instead:

>>> import unittest
>>> class NumbersTest(unittest.TestCase):
...   def test_odd(self):
...     for i in range(4):
...       with self.subTest(i):  # single positional arg
...         self.assertNotEqual(i%2, 0)
... 
>>> unittest.main(exit=False)
======================================================================
FAIL: test_odd (__main__.NumbersTest) (<subtest>)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 5, in test_odd
AssertionError: 0 == 0
======================================================================
FAIL: test_odd (__main__.NumbersTest) [2]
----------------------------------------------------------------------
Traceback (most recent call last):
  File "<stdin>", line 5, in test_odd
AssertionError: 0 == 0
----------------------------------------------------------------------
Ran 1 test in 0.001s
FAILED (failures=2)

This is because subTest() accepts a positional "msg" arg, passes it to _SubTest (Lib/unittest/case.py:515), and then _SubTest checks using "if self._message:" (Lib/unittest/case.py:1400).

I think it would be better to check the message against a sentinel value instead.

----------
components: Library (Lib)
keywords: easy
messages: 254827
nosy: ezio.melotti, michael.foord, pitrou, rbcollins
priority: normal
severity: normal
stage: test needed
status: open
title: Confusing output for TestCase.subTest(0)
type: behavior
versions: Python 3.4, Python 3.5, Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue25651>
_______________________________________


More information about the New-bugs-announce mailing list