Using CxxTest for Unit Testing

The project I’m currently working on is possibly the worst project I have _ever_ worked on. Not because it’s a bad idea – but because of the way we have to work on it.

There are 3 main parts:

  • Get an undocumented proprietary ArcIMS webservice (which no one knows anything about) to show some of our data
  • Add a button without access to the code
  • Add the new versions of a couple of our projects. This requires a conversion DLL as the interface to one has changed.

Gripes aside I’m currently working on the conversion DLL (as I’ve already beaten ArcIMS to a small bloody pulp with a large metal stick), more precisely on testing that it actually works.

I know the functions line up cos I’ve used dependancy walker so now it’s time for function testing and this is where the interesting bit comes.

I’ve used a prewritten test harness!!

Yes. I am not going to re-invent the wheel.

Anyways – on to the ‘review’.

[CxxTest] is great because it only uses source and header files – you don’t have to build it into a DLL at all (nice and portable).

The downside to this is that you need [Perl] but it’s not exactly tough to get hold of.

You basically write tests as classes in header files with each test function prefixed by the word test. Eg

void testMe()

Will result in this function being called by the test harness but doing:

void doNotTestMe()

won’t.

All you need to do after that is to include a small piece of code in your pre-build step like the following:

C:\Perl\bin\perl -w cxxtestgen.pl –error-printer -o MainFunction.cpp TestFile1.hpp TestFile2.hpp

Where MainFunction.cpp is where you want main to end up and the TestFile*.hpp’s are your defined test classes.

Maybe you could define a clever macro do add the classes for you but it’s only a small project so I’m not really interested.

In conclusion I’ve found CxxTest to be excellent :)

Technorati Tags: , , ,