Unit testing - Using Java Mock Objects in Python?

Chermside, Michael mchermside at ingdirect.com
Thu Nov 14 13:03:33 EST 2002


> > It would be great to have available the Java Mock classes available to 
> > be used from Python.
> 
> Could you describe shortly what makes Mock valuable for you?
> I wasn't able understand the distinct features of Mock Objects from
> the web site.  Is it that the framework manage deploying stubs rather 
> than the real code? 

Short answer: yes. Long explanation for those not familiar with it:

JUnit, and it's close partner, Python's unittest module, are both tools
that help you to EXECUTE your unit tests. They handle issues like
how to run a bunch of tests at once, how to set up for and clean up
after the tests, how to declare that you expect a certain result during
a test, or that you expect an exception to be thrown.

JUnit and unittest have lots of things that they DON'T do. For instance,
they DON'T automagically write the tests for you (you have to actually
write some lines of code to specify each test you want to run). They
don't track coverage (to ensure every line of code or every possible
code branch gets executed), although this might be possible with
ADDITIONAL libraries.

One other problem they don't solve is that of simulating external
services. Suppose you write a program (in Java OR Python... the principle
is the same) which reads from and writes to an Oracle database. Now,
your unit tests need to test out code like "FindUserInDb()", and
"DeleteAllUsersFromDB()". But if you REALLY invoke DeleteAllUsersFromDB
on the real DB instance, it could be disasterous. The ideal solution
might be to set up a separate instance of the database just for
testing, but that makes for AWEFULLY slow setup()/teardown() methods
and may be downright impossible (Oracle licenses aren't cheap!). You
can easily see that the same problem extends to other external services
besides Oracle databases.

The usual design pattern for addressing this is called "mock objects".
Basically, instead of using a REAL database connection object, you use
a "fake" object, which simply remembers the arguments passed to it
(so you can check to make sure they are what was expected by the test)
and returns "rigged" outputs (so you can make the test go the way you
wanted it to go). This kind of thing is actually QUITE EASY to do in
Python, because the design of the language makes it easy to write
generic handlers (using __getattr__, __setattr__, and friends). In Java
it's a bit harder, but the folks at www.mockobjects.com have written
some fairly good ones and they accept contributions.

As for the original question, I don't know of a way to use java mock
objects to debug Jython (or python) code. But there are a few places
it could still be useful. One example would be the above... if your
Jython code is accessing a database via JDBC, you could stub it out 
using the mockobjects JDBC mock classes. But to create a mock of a
Python class, you're better off writing it in Python... it'll be
easier to write, and more powerful to boot (better introspection and
flexibility via __getattr__ & friends).

-- Michael Chermside




More information about the Python-list mailing list