At the end of my previous article Pros and Cons of Using the C assert Macro for Unit Testing in C/C++ I promised to follow up with an article summarising the popular unit testing libraries/frameworks. In this article I will give that summary:
- For the .NET languages there is NUnit. Although the source code is written in C# it can be used to unit test any .NET language. Sample code is supplied for C#, .NET C++ extensions (both managed and CLR) and J#
- JUnit was developed for the unit testing of Java code and was the inspiration for many unit test frameworks that were to follow. JUnit is Java specific
- For Python there is the Python Unit Testing Framework or PyUnit for short. PyUnit is part of the Python standard library as of Python version 2.1
- For ISO standard C++ there are several unit test frameworks/libraries around. Probably the most popular are CPPUnit, The Boost Test Library and Aeryn. CPPUnit was an attempt to “port” JUnit to C++, whereas The Boost Test Library and Aeryn are in keeping with idiomatic modern C++
That concludes my roundup. Please comment if you feel I’ve missed a library that should have been included. Note that all the above libraries are open source (and in the case of Python, Python itself is open source).
Update
(Wed 11th Feb, 2009)
Thanks to Alan (see comments, below) for drawing my attention to SUnit – the unit testing tool for Smalltalk.
7 Comments
It is worth noting that JUnit was inspired by SUnit and, like your comments about CPPUnit, doesn’t fit well with Java idioms – this was addressed by TestNG (although I understand JUnit has begun to evolve since I last used it).
Alan,
Thanks for pointing out these things. I didn’t know about JUnit being inspired by SUnit. Actually I hadn’t heard of SUnit until your comment. I assumed Smalltalkers would have a unit test framework, so I probably should have looked it up and included it in the above article – I’ll append an update with a link to it.
Re your comment that JUnit “doesn’t fit well with Java idioms”. I don’t know much about Java, but that did surprise me! Could you elaborate a little if/when you get chance? In what way is/was JUnit counter-idiomatic in Java, and how could it work such that it does fit with Java idioms?
– Mark.
Hi Mark – maybe an idea for your next post. I’ve just run into a codebase with a disturbing metric…
grep “throw[^[:graph:]]*([^)]” `find -name \*.cpp -or -name \*.h` | wc -l
222
How about writing “The Only Good throw spec is an Empty One”?
Alan,
Sorry about the delay in responding, and yes that is more than a little worrying!
I see your code base is C++. Are you suggesting an article about throw specs in C++ or are you suggesing one that includes Java?
– Mark.
[No worries about the delay – I wasn’t even sure you’d approve my comment as being on topic for this article.]
Throw specifications in C++ and exception specifications in Java are very different beasts and treating them in the same article would only have value if the purpose were to explain the differences. That has been done.
In terms of utility I don’t know of anyone that would argue the C++ version is anything other than a failed experiement. In Java the case is less clear as there are some benefits. (Although it is telling that no comparable, subsequent language has adopted this approach.)
Alan,
[I assume you posted an off-topic comment because there was no logical place to post it, and no contact page for for sending me messages in general – I’ve now fixed this, you many notice a contact option has appeared on the main menue!]
Unfortunately my Java knowledge is very rusty, and my understanding of the throw spec issues in Java is pretty much nebulous 🙂
C++ however – as you (Alan) know – is a very different matter! Thanks for the suggestion. It may be a few days before the article appears, but I’ll start work on it!
– Mark.
In a mixed C++ (or C) with .NET then I’ve found using NUnit for testing everything that is public works pretty well – especially if you have to test COM API’s. Consistency across the unit test results and software you need to install is a big benefit, especially if the code will eventually be called from .NET.