Check Out Mox, Our Mock Object Framework for Python
Posted:
Wednesday, July 16, 2008
So you just finished writing a really sweet Python application, and now it's time to do some testing. Or wait, better yet, you're going to write the tests first, then write your application code. It's probably time to pick a mock object framework to make writing tests easy; you definitely don't want to waste your time writing mocks by hand! Well, after many requests from former interns and Googlers working on Open Source projects, I'm happy to announce that now you have another option: Mox!
Mox is a mock object framework for Python developed at Google (and used in hundreds of projects here) that uses the record-replay-verify paradigm you might already be familiar with from library's such as Java's EasyMock. You create a mock object based on a class, record the expected behavior by calling methods on the mock just like a regular instance, then you put the mock in replay mode and run your test. After your test is complete, you can verify that the code-under-test interacted with the mock as expected.
Why would you want / need a mock object framework? In many cases you want to be able to test portions of your code without setting up all of your applications dependencies (databases, file systems, or even just other complex parts of your application). Mox allows you to easily create mock versions of your dependencies (Python classes, even modules) for use in tests. These mock versions only need to know about how they are called (which methods with what parameters), and what they should return. For example, instead of starting up a local copy of MySQL and populating the database with test data just to test that your InsertCustomer method works (or fails elegantly), now you can use Mox to simulate calls to, and responses from the database.
Mox has several other advanced features, like the ability to temporarily stub out any object or module with a mock object — even parts of the global environment.
We always love to hear what you think. Please join the Mox discussion group and share your thoughts with us.
Mox is a mock object framework for Python developed at Google (and used in hundreds of projects here) that uses the record-replay-verify paradigm you might already be familiar with from library's such as Java's EasyMock. You create a mock object based on a class, record the expected behavior by calling methods on the mock just like a regular instance, then you put the mock in replay mode and run your test. After your test is complete, you can verify that the code-under-test interacted with the mock as expected.
Why would you want / need a mock object framework? In many cases you want to be able to test portions of your code without setting up all of your applications dependencies (databases, file systems, or even just other complex parts of your application). Mox allows you to easily create mock versions of your dependencies (Python classes, even modules) for use in tests. These mock versions only need to know about how they are called (which methods with what parameters), and what they should return. For example, instead of starting up a local copy of MySQL and populating the database with test data just to test that your InsertCustomer method works (or fails elegantly), now you can use Mox to simulate calls to, and responses from the database.
m = mox.Mox()
mock_db = m.CreateMock(database)
new_pk = 12356;
mock_db.OpenConnection()
mock_db.Execute("INSERT INTO Customers (fname, lname, email) VALUES
('Sandy', 'Smith', '[email protected]'").AndReturn(new_pk)
mock_db.CloseConnection()
m.ReplayAll()
p = PersistenceLogic(mock_db)
actual_pk = p.InsertCustomer('Sandy', 'Smith', '[email protected]')
m.VerifyAll()
assertEquals(new_pk, actual_pk)
Mox has several other advanced features, like the ability to temporarily stub out any object or module with a mock object — even parts of the global environment.
We always love to hear what you think. Please join the Mox discussion group and share your thoughts with us.

In that code sample, what is database? Can you extend the code sample to include relevant imports?
ReplyDeleteOur ancestors gave us doctests so we could write verifiably correct excerpts.
Matt
I'll definitely gonna give it a try. But before jumping to mocks, I am wondering if you can share, as information only, what unit testing framework are the you using. Coming from Java and being one of the co-creators of TestNG, I have found the unit test land in Python quite old or stale. So far, I've looked at unitest and TestOOB. The first one is pretty 'restrictive' and quite impossible to be extended. The later seems stale. So, I would really appreciate any hints from you.
ReplyDeletecheers,
Alex Popescu
@Alex:
ReplyDeleteTake a look at nose [1] its development is very active, also take a look at this page [2], there is a python testing tools list [3], finally keep an eye on these two blogs [4][5].
[1] http://www.somethingaboutorange.com/mrl/projects/nose/
[2] http://pycheesecake.org/wiki/PythonTestingToolsTaxonomy
[3] http://lists.idyll.org/listinfo/testing-in-python
[4] http://ivory.idyll.org/blog
[5] http://agiletesting.blogspot.com/
@Alex
ReplyDeleteI use Pythons unittest.
@Matt
ReplyDeleteIt's just a toy example for a Blog post, so there aren't imports. There are more examples on the wiki, if you're interested, and I can flesh them out to be more concrete, if that would be helpful. :)