Skip to content

Warn when no specs are used #21

Open

Description

@RonnyPfannschmidt

Mock without specs is so forgiving that people often write tests whose asserts do nothing

This is mainly because the apu of mock is severely flawed and error-prone

Activity

  1. The-Compiler commented on Sep 29, 2015

    @The-Compiler
    Member

    I agree, but I don't think it's the job of pytest-mock to fix this up.

    I plan to write a pylint plugin for this some day, to statically check mocks are always used with spec or autospec.

    I wonder, is there a better mocking library (i.e. with a more sane API) which could be exposed to pytest?

  2. RonnyPfannschmidt commented on Sep 29, 2015

    @RonnyPfannschmidt
    MemberAuthor

    There is none, generally mocking is tricky and a sign of bad structure

    I wouldn't want to invent one

  3. nicoddemus commented on Sep 29, 2015

    @nicoddemus
    Member

    Hi @RonnyPfannschmidt,

    What do you suggest exactly? If one uses mocker.patch without passing autospec=True or a spec, to issue a pytest-warning?

  4. RonnyPfannschmidt commented on Sep 30, 2015

    @RonnyPfannschmidt
    MemberAuthor

    Yes

  5. nicoddemus commented on Sep 30, 2015

    @nicoddemus
    Member

    Fair enough. 😄

    I don't want current users to suddenly start to receive warnings for valid uses of the API though, so I'm thinking of making this an ini option:

    [pytest]
    mock_warnings = yes  # defaults to no

    @RonnyPfannschmidt, @The-Compiler, what do you guys think?

  6. RonnyPfannschmidt commented on Sep 30, 2015

    @RonnyPfannschmidt
    MemberAuthor

    👍

  7. chishaku commented on Dec 21, 2020

    @chishaku

    Thankful I found this issue.

    I'm new to mocking and I was wondering why the test wasn't failing when a mock was called more than once.

    I had

    mocked_function = mocker.patch('app.function')
    do_something()
    mocked_function.called_once()

    The test passed even though called_once isn't even a function.

    After I added autospec=True based on the contents of this issue, I am seeing the errors I'm expecting.

    +1 for the mock_warnings config option.

    Even an update to the docs would be very helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions