Unit testing - Using Java Mock Objects in Python? 
Author Message
 Unit testing - Using Java Mock Objects in Python?

Hi,

Has anybody already tried to re-use or port the Java implementation
of Mock ojects (available on www.mockobjects.com) to Python?

Is it possible to combine the Java Mock objects with python using Jython?

It would be great to have available the Java Mock classes available to
be used from Python.

Thanks
Joel



Sun, 01 May 2005 22:05:47 GMT  
 Unit testing - Using Java Mock Objects in Python?

Quote:

> Hi,

> Has anybody already tried to re-use or port the Java implementation
> of Mock ojects (available on www.mockobjects.com) to Python?

Counter-Question: Do you know about the python unittest package?  

Quote:
> Is it possible to combine the Java Mock objects with Python using Jython?

probably.

Quote:
> 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?

thanks,

    holger



Sun, 01 May 2005 23:26:58 GMT  
 Unit testing - Using Java Mock Objects in Python?

Quote:
> > 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



Tue, 03 May 2005 02:03:33 GMT  
 Unit testing - Using Java Mock Objects in Python?

Quote:

> Has anybody already tried to re-use or port the Java implementation
> of Mock ojects (available on www.mockobjects.com) to Python?

There is a pure python implementation of Mock.
Don't have a url but my version is about 2K in size. ;)

Quote:
> Is it possible to combine the Java Mock objects with Python using
> Jython?

> It would be great to have available the Java Mock classes available to
> be used from Python.

--
Max Ischenko, a kind of programmer at CJSC "Malva".


Fri, 06 May 2005 16:29:34 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Test::Unit::Mock: Mock objects for testing with Test::Unit

2. using test::unit for C++ unit tests

3. Unit testing or mocking Net::SMTP

4. ruby/mock and test::unit

5. Test-first and mock objects with PyOpenGL

6. ANNOUNCE: Python Mock Objects

7. Using Test::Unit to assert messages appeared on $stdout/$stde rr

8. Using Test::Unit to assert messages appeared on $stdout/$stderr

9. Using FxRuby and Test::Unit.

10. cast object of python to java object - problem

11. TriZPUG November Meeting: Unit Testing in Python

12. Need help Translating Python unittest to Ruby Test::Unit

 

 
Powered by phpBB® Forum Software