How to Mock a name attribute?

In Python, mocking objects is quite easy but sometimes it can become confusing and frustrating. It needs a bit of practise and when you master it – it’s a very powerful (and a must-have) tool to make your tests better. It all works fine until you try to mock a name attribute then things get a bit weird and tricky.

Simple Mock example

This came out as I recently worked a lot with gcloud library. This is required by our own code, which means to test it we had to mock few things (actually, a lot…). Everything was going fine until I had to mock .name  attributes in some classes. The very simple  MagicMock(name='foo') won’t work as expected.

Let’s start with a simple mock example

What we do is simply creating a Mock object with some attributes. Let’s see the output:

Hm, it’s not what you’d expect right? Why the name is not what we’ve defined while creating an object? property

This happens because the name is an argument to the Mock constructor and you can’t just pass it in at creation time.

According to the docs:

name: If the mock has a name then it will be used in the repr of the mock. This can be useful for debugging. The name is propagated to child mocks.

Simply said name parameter is just used for easier debugging. This means we can’t just set it. There must be a way and there’s more than 1!

 Setting a name after mock creation

In my opinion this is the simplest solution and all you need to do is set a name after mock creation, just like that:

That’s it! All good.


The second approach is to use the configure_mock() method. The code looks like this:


I find this solution as the most “magical” and don’t use it. I feel it’s a bit hacky. In this method you have to use a PropertyMock() class to attach a property to mock object. See why I think it’s not “pretty”:

As you can see there’s magic happening here. We’re attaching it to the mock type object.

Leave a Reply

Your email address will not be published. Required fields are marked *