Python Mock Objects vs. Dummy Classes

by Jeremy Jones

I've seen at least two mock object libraries for Python (here and here). But I wonder, what is the benefit of using a simpler mock object over creating your own dummy class? It seems that if I created my own dummy class (a class which implements the same interface that it is attempting to "mock"), I would have tighter control over the behavior of the thing as well as have a nicer re-use experience. It seems that mock objects are typically defined on the fly and then thrown away. I'm sure you could re-use them, but if they are really intended to be use-once-then-throw-away, it may be a little harder.

I guess what I'd really like to see is something between mock objects and dummy classes, something where you define a dummy class to be used as one of your application classes, but which also contains the convenience methods that the two mock libraries have. Suggestions, anyone?

2 Comments

Lawrence Oluyede
2007-07-23 12:35:53
I think Fowler explains the difference in a clear way in this article: http://martinfowler.com/articles/mocksArentStubs.html
chromatic
2007-07-31 12:07:54
I struggled with this for a while and finally wrote Test::MockObject::Extends after two false starts. It's not as useful in a test framework without something like Test::Builder, however.