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

PyCon 2018 Call for Proposals is Open!

It’s here! PyCon 2018’s Call for Proposals has officially opened for talks, tutorials, posters, and education summit presentations. 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.

Before we dive in, the deadlines:
Tutorial proposals are due November 24, 2017.Talk, Poster, and Education Summit proposals are due January 3, 2018.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 need beginner, intermediate, and advanced proposals on all sorts of topics. We also need b…

PyCon 2018 Launches New Site, Sponsorship Search

After two great years in Portland, PyCon is shipping off to Cleveland for the 2018 and 2019 renditions of the Python community's largest gathering. PyCon 2018 will take place May 9 through 17 with two days of tutorials, three days of talks, and four days of development sprints.

For more information, check out our newly refreshed website at https://us.pycon.org/2018/ and follow us here on the blog and at @pycon on Twitter.

New Website

The new site features a design centered on the historic landmark Terminal Tower, a 52 story skyscraper that overlooks downtown Cleveland. When it opened in 1930, the tower was the fourth tallest building in the world and the tallest building outside of New York City. Though its height no longer tops the charts, the tower and surrounding Tower City area remain highly important to the city. What once was a beacon to guide ship captains to Cleveland's port and airplane pilots to its airport, the tower now includes 508 LEDs that light up for the holida…

How to get ready for the PyCon development sprints

[A guest post by Kushal Das, one of the 2016 Sprint Coordinators]So — you have already decided to join in the PyCon development sprints! The sprints run for four days, from Thursday to Sunday after the conference. You do not have to be registered for the conference to attend the sprints! Some teams plan to write code over all four days, while some projects plan a shorter sprint if the organizers cannot stay for all four days.Can you start getting prepared for the sprint ahead of time? Yes!There are several things you can do ahead of time, that can save effort once you arrive at the sprints — and some preparations can even be made at home, before you arrive at PyCon:Have your operating system updated and patched — whether Mac, Windows, or Linux. This eliminates one possible source of problems with getting software up and running.Go ahead and install the version control system that will be used by the projects you are interested in. If you install both git and Mercurial on your computer…