pix
pix

The PyCon Blog

pix

Pycon 2011: Interview with Michael Foord

Friday, February 25, 2011

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!

0 comments:

 

Tags

Diaporama

Loading...