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

Warn when no specs are used #21

Open
RonnyPfannschmidt opened this issue Sep 29, 2015 · 7 comments
Open

Warn when no specs are used #21

RonnyPfannschmidt opened this issue Sep 29, 2015 · 7 comments

Comments

@RonnyPfannschmidt
Copy link
Member

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

@The-Compiler
Copy link
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?

@RonnyPfannschmidt
Copy link
Member Author

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

I wouldn't want to invent one

@nicoddemus
Copy link
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?

@RonnyPfannschmidt
Copy link
Member Author

Yes

@nicoddemus
Copy link
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?

@RonnyPfannschmidt
Copy link
Member Author

👍

@chishaku
Copy link

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
Projects
None yet
Development

No branches or pull requests

4 participants