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

Better error messages in unittest.mock #79228

Closed
PetterS mannequin opened this issue Oct 23, 2018 · 8 comments
Closed

Better error messages in unittest.mock #79228

PetterS mannequin opened this issue Oct 23, 2018 · 8 comments
Labels
3.8 only security fixes stdlib Python modules in the Lib dir

Comments

@PetterS
Copy link
Mannequin

PetterS mannequin commented Oct 23, 2018

BPO 35047
Nosy @vstinner, @voidspace, @eamanu, @PetterS, @tirkarthi
PRs
  • bpo-35047: Better error messages in unittest.mock #10090
  • bpo-35047: Update whatsnew/3.8. #13746
  • 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 2018-10-28.20:42:15.416>
    created_at = <Date 2018-10-23.08:35:35.824>
    labels = ['3.8', 'library']
    title = 'Better error messages in unittest.mock'
    updated_at = <Date 2019-06-04.19:35:08.173>
    user = 'https://github.com/PetterS'

    bugs.python.org fields:

    activity = <Date 2019-06-04.19:35:08.173>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2018-10-28.20:42:15.416>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2018-10-23.08:35:35.824>
    creator = 'Petter S'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 35047
    keywords = ['patch']
    message_count = 8.0
    messages = ['328292', '328344', '328378', '328379', '328386', '328743', '328745', '344636']
    nosy_count = 5.0
    nosy_names = ['vstinner', 'michael.foord', 'eamanu', 'Petter S', 'xtreak']
    pr_nums = ['10090', '13746']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue35047'
    versions = ['Python 3.8']

    @PetterS
    Copy link
    Mannequin Author

    PetterS mannequin commented Oct 23, 2018

    When developing unit tests with unittest.mock, it is common to see error messages like this:

    AssertionError: Expected 'info' to not have been called. Called 3 times.
    

    It would be really helpful if those 3 calls were listed in the assertion error.

    I am happy to add this if people agree it is a good thing.

    @PetterS PetterS mannequin added 3.8 only security fixes stdlib Python modules in the Lib dir labels Oct 23, 2018
    @serhiy-storchaka serhiy-storchaka changed the title Better error messages un unittest.mock Better error messages in unittest.mock Oct 23, 2018
    @eamanu
    Copy link
    Mannequin

    eamanu mannequin commented Oct 23, 2018

    It seems to be a good idea.

    If you attach a sample, it would be great.

    @PetterS
    Copy link
    Mannequin Author

    PetterS mannequin commented Oct 24, 2018

    Sure, consider the following code:

    from unittest import mock
    
    m = mock.Mock()
    m(1, 3)
    m("Test", data=[42])

    When I call

    m.assert_not_called()

    I get the following error message:

    AssertionError: Expected 'mock' to not have been called. Called 2 times.

    This is what I would like to improve. On the other hand, if I call

    m.assert_has_calls(mock.call(3))

    I get the following error:

    AssertionError: Calls not found.
    Expected: ['', (3,), {}]
    Actual: [call(1, 3), call('Test', data=[42])]

    This is great and may serve as a template for what I want to introduce.

    @PetterS
    Copy link
    Mannequin Author

    PetterS mannequin commented Oct 24, 2018

    (The example above should have been "m.assert_has_calls([mock.call(3)])" but it does not affect my point.)

    @eamanu
    Copy link
    Mannequin

    eamanu mannequin commented Oct 24, 2018

    I think that is a good change.

    Maybe you can apply the change on 3.6, 3.7 and 3.8

    @vstinner
    Copy link
    Member

    New changeset 47d9424 by Victor Stinner (Petter Strandmark) in branch 'master':
    bpo-35047, unittest.mock: Better error messages on assert_called_xxx failures (GH-10090)
    47d9424

    @vstinner
    Copy link
    Member

    Thanks Petter Strandmark!

    @vstinner
    Copy link
    Member

    vstinner commented Jun 4, 2019

    New changeset 001d63c by Victor Stinner (Petter Strandmark) in branch 'master':
    bpo-35047: Update whatsnew/3.8 for better mock error message (GH-13746)
    001d63c

    @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.8 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant