pytest-mock icon indicating copy to clipboard operation
pytest-mock copied to clipboard

Warn when no specs are used

Open RonnyPfannschmidt opened this issue 10 years ago • 7 comments

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

RonnyPfannschmidt avatar Sep 29 '15 04:09 RonnyPfannschmidt

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?

The-Compiler avatar Sep 29 '15 05:09 The-Compiler

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

I wouldn't want to invent one

RonnyPfannschmidt avatar Sep 29 '15 05:09 RonnyPfannschmidt

Hi @RonnyPfannschmidt,

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

nicoddemus avatar Sep 29 '15 23:09 nicoddemus

Yes

RonnyPfannschmidt avatar Sep 30 '15 04:09 RonnyPfannschmidt

Fair enough. :smile:

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?

nicoddemus avatar Sep 30 '15 11:09 nicoddemus

:+1:

RonnyPfannschmidt avatar Sep 30 '15 12:09 RonnyPfannschmidt

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

chishaku avatar Dec 21 '20 16:12 chishaku