Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Display actual calls for unittest.assert_has_calls even if empty #113569

Closed
@wookie184

Description

@wookie184

Feature or enhancement

Proposal:

Running this:

import unittest
from unittest.mock import Mock, call

class TestCase(unittest.TestCase):
    def test_thing(self):
        mock = Mock()
        mock.assert_has_calls([call(a=1)])

unittest.main()

currently gives this test failure message:

AssertionError: Calls not found.
Expected: [call(a=1)]

This message does not imply that there were no calls. I propose that the test failure message is instead this:

AssertionError: Calls not found.
Expected: [call(a=1)]
  Actual: []

The "Actual" list is already displayed if there are calls. It's only excluded if there are no calls.


The logic that causes an empty list not to be displayed is in the _calls_repr method. For the other usages - assert_called_once_with, assert_called_once, and assert_not_called - the number of calls is displayed directly in the error message so it already says that there are no calls. This makes me think the current behaviour of assert_has_calls is just an oversight rather than intentional behaviour.

I'd be happy to open a PR for this.

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions