Skip to content
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

Closed
ezio-melotti opened this issue Nov 18, 2015 · 5 comments
Closed

Confusing output for TestCase.subTest(0) #69837

ezio-melotti opened this issue Nov 18, 2015 · 5 comments
Labels
3.7 (EOL) end of life easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ezio-melotti
Copy link
Member

BPO 25651
Nosy @pitrou, @rbtcollins, @ezio-melotti, @voidspace, @berkerpeksag, @zware
PRs
  • [Do Not Merge] Convert Misc/NEWS so that it is managed by towncrier #552
  • Files
  • subtest_msg_check_against_None.patch
  • Note: 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:

    assignee = None
    closed_at = <Date 2016-09-21.16:36:57.463>
    created_at = <Date 2015-11-18.02:34:13.067>
    labels = ['3.7', 'easy', 'type-bug', 'library']
    title = 'Confusing output for TestCase.subTest(0)'
    updated_at = <Date 2017-03-31.16:36:35.079>
    user = 'https://github.com/ezio-melotti'

    bugs.python.org fields:

    activity = <Date 2017-03-31.16:36:35.079>
    actor = 'dstufft'
    assignee = 'none'
    closed = True
    closed_date = <Date 2016-09-21.16:36:57.463>
    closer = 'berker.peksag'
    components = ['Library (Lib)']
    creation = <Date 2015-11-18.02:34:13.067>
    creator = 'ezio.melotti'
    dependencies = []
    files = ['41115']
    hgrepos = []
    issue_num = 25651
    keywords = ['patch', 'easy']
    message_count = 5.0
    messages = ['254827', '255064', '256574', '277170', '277171']
    nosy_count = 8.0
    nosy_names = ['pitrou', 'rbcollins', 'ezio.melotti', 'michael.foord', 'python-dev', 'berker.peksag', 'zach.ware', 'Nan Wu']
    pr_nums = ['552']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25651'
    versions = ['Python 3.5', 'Python 3.6', 'Python 3.7']

    @ezio-melotti
    Copy link
    Member Author

    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.

    @ezio-melotti ezio-melotti added stdlib Python modules in the Lib dir easy type-bug An unexpected behavior, bug, or error labels Nov 18, 2015
    @NanWu
    Copy link
    Mannequin

    NanWu mannequin commented Nov 21, 2015

    Made it check against None explicitly. My concern is if [] is passed in, if will show [[]]. But this case should be rare.

    @zware
    Copy link
    Member

    zware commented Dec 17, 2015

    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 '[[]]'.

    @python-dev
    Copy link
    Mannequin

    python-dev mannequin commented Sep 21, 2016

    New changeset ba743894e793 by Berker Peksag in branch '3.5':
    Issue bpo-25651: Allow falsy values to be used for msg parameter of subTest()
    https://hg.python.org/cpython/rev/ba743894e793

    New changeset ddbf92168a44 by Berker Peksag in branch '3.6':
    Issue bpo-25651: Merge from 3.5
    https://hg.python.org/cpython/rev/ddbf92168a44

    New changeset e5888f5b9cf8 by Berker Peksag in branch 'default':
    Issue bpo-25651: Merge from 3.6
    https://hg.python.org/cpython/rev/e5888f5b9cf8

    @berkerpeksag
    Copy link
    Member

    Fixed. I lost some time because of this today :)

    @berkerpeksag berkerpeksag added the 3.7 (EOL) end of life label Sep 21, 2016
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life easy stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants