Farm Development

Fudge: Another Python Mock Framework

I'm excited to announce the release of Fudge, a Python module for replacing real objects with fakes (mocks, stubs, etc) while testing.

Fudge started when a co-worker introduced me to Mocha, a mocking framework for Ruby and a simpler version of jMock (Java). Up to that point I had been building mocks "by hand" in Python with a post mortem approach; I'd set up some fakes then inspect their call stacks at the end of the test. I like the jMock approach better—you declare a fake object that expects methods and arguments, you replace the real object with the fake one, run your code, and you're done. If your code doesn't live up to the expectations then your test fails.

You can see some code examples here or read about why I added yet another mock framework to the Python landscape. There is also a JavaScript port I'm working on but that's not fully complete yet.

Mock objects can be dangerous since the declaration part isn't tied to reality but when used correctly they can make for a fast and efficient test suite. Also, they are essential when testing code that calls out to an external web service or does other Internet things like send email. This is what I'm mainly using mock objects for.

  • Re: Fudge: Another Python Mock Framework

    Sphinx rocks.

    I commend you for adding examples and the why section, and mentioning them upfront in this post.

    I wonder, though, if the "complete test module" example is missing a fudge.stop() in the test function? The ease of omitting, and also the asymmetry of calling 'start' and 'stop' in different functions hint to me that the name might be wrong... If it were called fudge.verify(), it would obviously be something that you call inside a test.

  • Re: Fudge: Another Python Mock Framework

    Have you heard about Mox, the Python mock library by Google? It's not mentioned in your "why" page.

    Looking at code examples, I much prefer Fudge's PEP-8 compliant lowercase_with_underscores method names to Mox's ForeignAccentStudlyCaps.

  • Re: Fudge: Another Python Mock Framework

    Hi Marius. I have a vague memory of coming across mox in my research but I think I was confused by the replayAll() method and thought it used the record/playback strategy. I also don't recall there being very good docs but I just found a pretty good usage page on their google code wiki. I'll have to try it out. At first glance, I prefer the shorter method names (like Mocha) but I do like the approach.

    And you make a good point about fudge.stop() vs. fudge.verify(), I may add that and deprecate the other.

  • Re: Fudge: Another Python Mock Framework

    The irony of the ease of implementing *anything* in Python - it's often easier to roll-your-own than it is to find an existing library that does what you want. Case in point: Fudge. It has no visible advantage over the more mature mocker (http://labix.org/mocker), but I'm assuming you didn't find it when you went looking for alternatives. I can't fault your initiative, but I would encourage you to check pypi the next time you want to write a library. There are lots of problems that could benefit from your attention that haven't already been been solved.

  • Re: Fudge: Another Python Mock Framework

    Hi Maru. Thanks for taking the time to send some info about mocker. I had searched for Python mock frameworks but you're right I must have missed this one.

    I have added mocker to my why-another-mock-framework section:

    http://farmdev.com/projects/fudge/why-fudge.html

    One thing I feel that Fudge has over mocker is that you can declare expectations up front in readable code instead of having to record them for later playback.

    It all boils down to personal preference but I think it's fine to have many different mock frameworks available for users. This empowers developers as there are many choices.

  • Re: Fudge: Another Python Mock Framework

    Hi Kumar,

    I agree that declaring expectations up front is useful, but I would prefer to have that functionality added to an existing mocking library than to have to use an entirely new one. If other mocking libraries find that feature useful, they are going to have to roll their own version rather than have you work with them to implement it Call my crazy, but collaboration and integration would seem a more reasonable way of allocating oss development resources for common software than divergence and competition. I can only hope that services such as launchpad and github are lowering the bar for that sort of cooperation.

  • Re: Fudge: Another Python Mock Framework

    As a Newbie, I find your article very helpful. I'm currently learning it in detail.

Note: HTML tags will be stripped. Hit enter twice for a new paragraph.

Recent Projects

  • JSTestNet

    Like botnet but for JS tests in CI.

  • Nose Nicedots

    Nose plugin that prints nicer dots.

  • Fudge

    Mock objects for testing.

  • Fixture

    Loading and referencing test data.

  • NoseJS

    Nose plugin that runs JavaScript tests for a Python project.

  • Wikir

    converts reST to various Wiki formats.