Announcing the Forge Mocking Library

I assume most of the people interested in testing code (which should be just about anyone writing code) is familar with the concept of Mocking. It is a pretty powerful concept, and although not always feasible depending on the language and/or circumstances, when used leads to more confidence in code and collaborations within it.

Python, of course, is a language in which mocking is relatively easy, and I have used several mocking frameworks in the past, trying to find the ideal one. There were several options that popped up quite quickly (ruling out some lesser known implementations that didn't seem maintained enough)

This brings me to Mox, apparently maintained by Google. I really like Mox all-in-all: it works, it has a relatively clean syntax and interface and it is simple to learn. I also found the documentation they included sufficient.

I used Mox for a while, but as time passed I grew more and more annoyed at some quirks:

  1. Mox takes a very weird approach to getattr, and attribute getting in general. For instance, when you use a MockAnything object, all getattr's work on it without any problem as long as you don't call them. This causes some weird bugs to slip by, if you get attributes that don't exist.

  2. What's that you say? don't use 'MockAnything'? ok. This brings me to some more quirks. If you use a 'regular' class mock, working with properties and attributes becomes pretty annoying really soon, because you have to pass them all in the mock constructor. This might not be pleasant in some cases.

  3. The maintainers are REALLY slow accepting contributions. For instance, I suggested they added a 'Is' comparator (the absence of which, given they have almost every comparator you can imagine, is quite surprising) -- something that hasn't happened yet although the patch is less than 20 lines of code.

  4. There is some versioning mixup - the project is maintained in Google Code, and for some reason no one has published a version to PyPI since 0.5.0, while the latest version was 0.5.3 when I started using it. This caused code I wrote to break when people just used easy_install.

  5. They don't support context managers. Even if you use a workaround and record enter and exit, it won't work with Python 2.7, in which special methods must be class-level members. This will cause more of your code to break on special methods when you migrate to 2.7.

This led me to write Forge. It is a new mocking library for python which aims at a simple interface, but flexible and powerful enough to make testing and mocking easier and more fluent. It is available here, and I guess there still is a lot of work to be done on it before I can consider it 'complete'.

Due to the harsh lesson I've learned about under-maintained projects, I'm going to try to be as responsive as possible to pull requests and feature suggestions - so don't hesitate to drop me a line if you want something (you can also open an issue in github, of course).

Comments

blog comments powered by Disqus