Skip to content

Inappropriateness in unittest documentation with actual behavior #118912

Description

@AliyevH

Documentation

https://docs.python.org/3/library/unittest.mock.html#auto-speccing

Your tests can pass silently and incorrectly because of the typo.

mock = Mock(name='Thing', return_value=None)
mock(1, 2, 3)
mock.assret_called_once_with(4, 5, 6)  # Intentional typo!

Your tests can pass silently and incorrectly because of the typo.


Expected that tests will pass as written in doc.

But actually script fails with raising error.

raise AttributeError(
AttributeError: 'assret_called_once_with' is not a valid assertion. Use a spec for the mock if 'assret_called_once_with' is meant to be an attribute.. Did you mean: 'assert_called_once_with'?

Am i missing something? I have tested this in Python3.11 and Python 3.12.
Looks like documentation must be updated or script must work as said in documenation.

Thanks for attention

Linked PRs

Activity

  1. blhsing commented on May 16, 2024

    @blhsing
    Contributor

    Note that the doc describes the pre-Python 3.5 behavior. Since Python 3.5, one has to enable the unsafe option to achieve the same behavior:

    :exc:`AttributeError`. Passing ``unsafe=True`` will allow access to

    unsafe: By default, accessing any attribute whose name starts with assert, assret, asert, aseert or assrt will raise an AttributeError. Passing unsafe=True will allow access to these attributes.

    Added in version 3.5.

  2. shaunagm commented on May 20, 2024

    @shaunagm
    SponsorContributor

    I am working on this issue! I hope to finish it today (at the PyCon sprints)

  3. added a commit that references this issue on May 20, 2024
  4. added 2 commits that reference this issue on May 20, 2024
  5. added 2 commits that reference this issue on May 20, 2024
  6. added a commit that references this issue on Jul 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions