Mock object bug with assert_not_called (Steve D'Aprano)

Steve D'Aprano steve+python at
Wed Dec 28 20:14:04 EST 2016

On Thu, 29 Dec 2016 10:36 am, Diego Vela wrote:

> Re: mock bug.
> Python 2.7.8
> I'll provide a sketch of the code since it is from work and I'm not
> allowed to share it directly.

We don't want to see your entire code base. We want to see the smallest,
simplest example that demonstrates the problem. Something we can copy and
paste into a file and run.

> Sketch of Code:
> @patch('emails.emails.render_to_string')
> def test_send_email(self, render):
>     context = {
>           'key': value
>     }
>     emails.send_email() # calls render_to_string
>     render.assert_any_call(
>          'template.txt',
>          context
>      )
>     render.assert_not_called()

This is, I'm afraid, useless. What is "patch"? What is "emails"? What class
does this belong to? What argument "render" should we pass? Where
does "value" come from?

It might help for you to read this:

It is written for Java developers, but the same rules apply here. Your
example code should be 

- as short and simple as you can make it (e.g. since this issue has nothing
to do with sending email, your code should not send email -- a simple print
should be enough for output);

- complete (no missing dependencies, all necessary modules are imported, no
uninitialised variables, functions called with all necessary arguments

- correct (the code runs and does what you say it does).

Thank you,

“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

More information about the Python-list mailing list