Skip to main content

Pycon 2011: Interview with Michael Foord

By Mike Driscoll

PyCon is only a few weeks away, so I decided to interview one of the speakers for this year’s convention. In this discourse you can learn about Michael Foord’s talk about mock and what he likes about PyCon in general. Michael Foord is probably best known for his work in the IronPython community and for writing a book on IronPython. He is also the author of the mock library.

1) What do you want the attendees to take away from this talk?

My talk is on mock, which is a mocking and patching library for testing with Python. It grew out of a very simple, 30 line, proof of concept I developed for Resolver Systems to replace the myriad stubs we were creating throughout our test suite. It has since grown to become one of the most popular mocking libraries, out of the many available. The main reason for this is because it's very *simple* to use. It *isn't* a framework and it isn't opinionated about how you write your tests. mock aims to simplify many of the common patterns for mocking and monkey patching when testing with Python.

This talk is an introduction to mock, so it isn't an advanced talk but will show people how mock solves the common problems for which it is designed. There are also some really interesting features coming in mock 0.7.0 (which you can use right now - the beta is up on PyPI). This includes the much demanded support for mocking objects that have "magic methods" (protocol methods) including context managers and so on.

So I guess I'd like to showcase the new features (Python 3 support - yay!) and hopefully show people who aren't familiar with mock not just why they should use mock but how.

One of the other topics I'll talk about, perhaps surprisingly, is when *not* to mock. There are certain streams of thought in the testing world that you should mock absolutely everything. This can make your tests fragile and mean that you end up testing against implementation (and are therefore subject to every implementation detail) rather than testing the purpose of your code. I'm a believer in testing units of behaviour rather than units of implementation. I'll talk a little bit about *minimising* the use of mocking within your tests and not straying into over-mocking that is going to make your tests painful to maintain.

2) What made you decide to speak on this topic?

Despite mock being pretty popular I've never spoken about it at conferences before. I like to contribute to PyCon by talking about *something* (if I'm wanted of course) so it seemed an obvious topic. Not only that, but mock is something I'm using daily (thankfully my team at Canonical were using mock even before I arrived) and seems to be proving useful to other people.

One of the reasons people sometimes don't test is because it can look like some things are harder to test than they really are. Certainly if you're not doing any mocking, and even possibly if you are, mock can make writing tests easier and remove another barrier (excuse) for not testing.

I'd also like to speak up in favour of the much maligned monkey-patch. Monkey patching (changing behaviour of objects at runtime by replacing members - usually methods) has a bad reputation in the Python world, and certainly for production code it is a great way to confuse people trying to read your code. For testing though it is a very useful technique for analysing the behaviour of your code, especially where certain code paths are "expensive" (hit the network, filesystem or database for example) or are complex to configure.

Some people religiously avoid monkey patching and advocate techniques like dependency injection or inversion of control. Whilst these can be very useful architectural decisions in their own right they are rarely needed in Python *purely for testability* (whereas in languages like Java and C# they absolutely are) and I'll be showing why.

3) Why did you write the mock module?

I kind of answered that already. Many of the existing mocking libraries are really framework; they encourage or force you to write your tests in a particular way. For mocking frameworks that follow the expect -> verify (or record -> replay) this puts the tests what feels like the wrong way round; you effectively make your assertions and then execute your code. They also force you to jump through hoops where you *aren't* interested in how some mock is used (or some aspect of it is used) but a particular test is really just about one small part of the behaviour of the code under test.

I thought we could do better and wrote a small Mock class that could replace pretty much all of the many stub objects we were using in our tests at Resolver Systems. Functionality grew as we and other people started to use it more and more.

4) What are you most looking forward to at PyCon this year?

I always look forward to meeting people and the hallway track. There are many people I interact with daily on IRC, twitter and mailing lists and only get to see in person at PyCon. The Python community is full of great people and I enjoy spending time with them. The talks are a nice bonus of course and now that I'm working daily with django (which I wasn't this time last year) I'll be seeing what new tricks and tools I can pick up on. Hearing about how the other implementations are doing, particularly pypy, is always fascinating too.

There are a whole lot of other great speakers that are coming to PyCon this year as well. This is just a very small taste of what you can see while you’re there. Also note that there will be lots of folks who don’t speak but who are quite possibly just as interesting. PyCon is creeping up on its attendance cap, so sign up now before it’s too late!

Comments

Popular posts from this blog

Hatchery Program Returns for 2019

PyCon is known around the world as the Python community’s premier event, attracting people from 39 countries. Outside of the main track of talks, PyCon is home to a growing number of events such as Young Coders, the Education Summit, Language Summit, Poster Session, and most recently the PyCon Charlas. The conference strives to be globally representative by promoting diversity and inclusion through these additional events and outreach programs. Our community works to approach these goals year on year. While we regularly receive requests to add events to PyCon, we have not had an established process for accepting and evaluating the community’s suggestions. By introducing the PyCon Hatchery Program in 2018, we took an initial step to introduce a long term process for evolving PyCon. What is our goal?We want to support our community and enable them to add events to PyCon that are important to our community. The long-term goals of this program are to support and grow sustainable programs th…

Pycon 2019 Call for Proposals is Open!

The time is upon us again! PyCon 2019’s Call for Proposals has officially opened for talks, tutorials, posters, education summit presentations, as well as the hatchery program PyCon Charlas. PyCon is made by you, so we need you to share what you’re working on, how you’re working on it, what you’ve learned, what you’re learning, and so much more.

Please make note of important deadlines for submissions:
Tutorial proposals are due November 26, 2018.Talk, Charlas, Poster, and Education Summit proposals are due January 3, 2019.
Who should write a proposal? Everyone! If you’re reading this post, you should write a proposal. PyCon is about uniting and building the Python community, and we won’t advance as an open community if we’re not open with each other about what we’ve learned throughout our time in it. It isn’t about being the smartest one in the room, so we don’t just pick all of the expert talks. It’s about helping everyone move together. “A rising tide lifts all boats,” if you will.

We…

PyCon 2018 Code of Conduct Transparency Report

The PyCon Code of Conduct sets standards for how our community interacts with others during the conference. For 2018 the Code of Conduct underwent an extensive overhaul, our procedures for reporting and responding to incidents were improved, and our on-site methods were improved. You can read more about the updates for 2018 here. Cumulatively these changes were meant to improve the safety, welcoming nature, and overall inclusivity of PyCon. Based on initial responses, feedback, and incidents reported this year we feel that we made progress on those goals. A Code of Conduct without appropriate reporting and response procedures is difficult to enforce transparently, and furthermore a lack of transparency in the outcomes of Code of Conduct incidents leaves the community without knowledge of how or if the organizers worked to resolve incidents. With that in mind, we have prepared the following to help the community understand what kind of incidents we received reports about and how the PyCon…