Friday, November 12, 2004

using other language to write unit tests

Just encountered an article on IBM's devWorks which gives an example on how to use Groovy to write unit tests for Java code. And this reminded me of experience I once had with mixing languages in unit testing.

In a rather big project I once took part, we used an in-house Tcl-based framework to perform system tests via a network. Some other libraries developed for that project were coded in C++ and Lua. I decided to test this libraries as well using the same Tcl framework I've already built. I used SWIG to make C++ code accessible from within Tcl. I used the same trick to access C API of the Lua interpreter and feed it with Lua code. Just imaging this mess -- evaluate Lua code from within Tcl virtual machine (written in C) via generated dispatching code through the Lua virtual machine and down to C, at last!

It was kind of nice to be able to load a C++ .so library into Tcl shell, experiment with it, call various methods and play with results. But as for production usage, it didn't worked out.
There were two main reasons:
  1. Generation step always was a bit brittle. Even though SWIG managed to handle all C++ constructs we used it required some manual setup and it sometimes was hard to figure out how to use the generated code.
  2. Using different languages (and I'd add, so distinctively different languages) were putting a significant burden on a developer. It just felt awkward.
I don't even mention that instead of developer who is proficient with one language you need a developer who is proficient in both. Plus could master SWIG tool.

Groovy was designed to be as close to Java as possible and it's interoperability with Java is excellent, so unit tests for Java code written in Groovy won't suffer from the same problems I had. Still, I think it's better to stick to a single language for both, no matter how close Groovy is to Java. If you buy arguments for Groovy's simplicity - then just write both production code and unit tests in Groovy. That's my opinion.


Post a Comment

<< Home