farmdev

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.