Steve on Image Processing
February 3rd, 2009
MTEST – A unit test harness for MATLAB code
I just posted MTEST on the MATLAB Central File Exchange. MTEST is a unit test harness for testing MATLAB code. The design of MTEST is based on the popular xUnit architecture.
Now, writing test harnesses isn't exactly a central part of my day job, so let me describe how this project originated and evolved.
I first wrote my own test harness back in 2003 or so, when Digital Image Processing Using MATLAB was published. I needed an easy way to write and run tests for all the code in the book. The test harness we use at MathWorks has evolved over many years to support the needs of hundreds of developers and quality engineers, and dozens of products, so it was a little "big" for the needs of the book project. So I hacked together something simple that was just good enough to serve my own needs.
In 2006 I first presented my "Take Control of Your Code" talk at the International Conference on Image Processing. (I have since given versions of this talk at a half-dozen universities.) I described several tools and techniques intended to help "nonprogrammer" engineers and scientists be more productive with their coding. For testing code I suggested using a good test harness that makes writing and running tests as easy as possible. For testing MATLAB code, I recommended checking out the user-written test harnesses on the MATLAB Central File Exchange.
Then R2008a was released about a year ago, with dramatically improved language features for object-oriented programming. At about the same time I read the book Test-Driven Development, by Kent Beck. The book included a case study on using test-driven development to develop a test harness. And we were actively working on the second edition of Digital Image Processing Using MATLAB. Hey, it's a three-fer! A chance to learn the new language features, learn test-driven development techniques, and update the book's tests all at the same time.
Last spring I met Greg Wilson, co-editor of Beautiful Code, when he visited The MathWorks headquarters in Natick, MA. (Greg's articles on "software carpentry" partially inspired my "Take Control of Your Code" presentation.) Greg was interested in the experiences of engineers and scientists who do their programming in MATLAB, and he encouraged me to continue to develop and refine my MATLAB test harness. And he introduced me to Gerard Meszaros, author of xUnit Test Patterns. Both Greg and Gerard offered helpful architectural and design feedback (thanks, guys!), which I slowly worked to incorporate in the summer and fall. (Very slowly! This was a side project, remember.)
Finally last month I got to the point where I was willing to let people look at it and try it out. You can download it from the MATLAB Central File Exchange. Be sure to check out the extensive documentation. I just discovered that the File Exchange automatically made all of my html documentation files available online, without having to download and unpack the .zip file. That's very cool. The starting point for the documentation is the Readme.html file, and you might also want to look at "MTEST Quick Start - How to write and run tests."
I should point out that there are several other unit test harnesses available on the File Exchange. You can find them by searching for "unit test". I haven't looked at them in any detail, so I don't have particular recommendations. But if you don't like what I've done, take a look at the alternatives.
If you try MTEST, please let me know what you think. You can comment either here or on the File Exchange submission page.