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

Registration is Now Open!

The PyCon 2014 organizers are thrilled to announce the opening of registration for the April 9-17 conference, taking place at the Palais des congrès de Montréal in Montréal, Quebec, Canada. This event represents the first of two annual PyCons to take place in Montréal, following a hugely successful two-year run in Santa Clara, California.

As with years past, ticket prices remain unchanged, and value is ever increasing. At the close of talk and tutorial proposals last week, a record 565 talks were submitted -- over 100 more than for PyCon 2013. Reviewers have been hard at work to begin shaping the 2014 schedule, which is expected in December.

Quantity-based early bird rates are back for 2014, with the cap set at the first 800 tickets receiving the discounted rates. Total sales are initially being kept to 2,000 tickets. For an individual buyer, the regular $350 USD rate is cut 15% for during the early bird period to $300 USD. The $600 USD corporate rate is dropped to $450 USD during early…

Young Coder Tutorial Helps Daughter, Father Get Into Python

PyCon 2013’s “Change the Future” theme was a nod to Python’s growing use in education, and to devices like the Raspberry Pi and their targeted child audience. Before 2,500 attendees descended upon the Friday through Sunday conference, which gave each of them a Raspberry Pi, kids filled a lab for two days of free tutorials on the tiny computer that taught them the basics of Python. They, too, took home a Raspberry Pi.

Not only did the “Young Coder: Let’s Learn Python” tutorials lay the foundation for many children to go on and learn to program, they sent at least one father down that same path.

9-year-old Havana Wilson of Denver, Colo., made the trip to PyCon with her father, Bruce. After she showed interest in building video games, dad looked around the web for how to get her involved. “It was my job to turn her desire into action, so I did research on the most intuitive programming language that has the ability to produce games but also could be a wonderful gateway into programming,” h…

PyCon 2018 Registration is Now Open!

We’re thrilled to announce the opening of registration for PyCon 2018 in Cleveland, Ohio! The prior six PyCons have sold out, so prepare for another one and get your tickets early. The first 800 tickets sold are priced at an early bird discount, saving over 20% on corporate tickets and over 12% on individual tickets. Students save $25 if they purchase early!

To get started, create an account and head to https://us.pycon.org/2018/registration/ to get your tickets!

You get a package that is hard to beat when you register for PyCon. The conference itself is three days worth of our community’s 95 best talks, amazing keynote speakers each morning, and our famed lightning talks to close out each day, but it’s much more than that. It’s having over 3,000 people in one place to learn from and share with. It’s joining a conversation in the hallway with the creators of open source projects. It’s taking yourself from beginner to intermediate, or intermediate to advanced. For some, it’s getting st…