-
-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Confusing output for TestCase.subTest(0) #69837
Comments
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) ====================================================================== Traceback (most recent call last):
File "<stdin>", line 5, in test_odd
AssertionError: 0 == 0 ====================================================================== Traceback (most recent call last):
File "<stdin>", line 5, in test_odd
AssertionError: 0 == 0 Ran 1 test in 0.001s 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. |
Made it check against None explicitly. My concern is if [] is passed in, if will show [[]]. But this case should be rare. |
I think Ezio's suggestion of a sentinel value would be better, allowing None to be using as a legitimate 'message' [1]. That is, somewhere at global scope, define '_subtest_msg_sentinel = object()', change the msg default at Lib/unittest/case.py:500 to be 'msg=_subtest_msg_sentinel', and change the check at Lib/unittest/case.py:1400 to check 'if message is not _subtest_msg_sentinel'. [1] For example: class TruthTest(unittest.TestCase):
def test_truth(self):
for o in None, 1, 0, [], (4,):
with self.subTest(o):
self.assertTrue(o) Should print failure results including '[None]', '[0]', and '[[]]'. |
New changeset ba743894e793 by Berker Peksag in branch '3.5': New changeset ddbf92168a44 by Berker Peksag in branch '3.6': New changeset e5888f5b9cf8 by Berker Peksag in branch 'default': |
Fixed. I lost some time because of this today :) |
Misc/NEWS
so that it is managed by towncrier #552Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: