I have been writing unit tests using C#, Java, and Python using MbUnit, JUnit, and PyUnit, respectively. Since C++ does not fully support retrospection, unit testing on C++ can be tricky. Most C++ testing frameworks require the developer to 1) write a test case, then 2) register it — a two step process. This two-step approach carries a couple of problems. First, it is tedious to having write the test case, then register it. Second, the process is error-prone because of potential mismatch between the test case and the registration. In addition, the developer might forgot to register the test case, which exclude the test case from being run.
For those reasons, I like CxxTest for its simplicity: I only need to write the test case and be done with it. At first, I was putting off by CxxTest’s requirement: It relies on Perl or Python to generate registration code. However, I decided to press on and give it a try and the result was worth it.
The following are the steps I performed to get CxxTest working. While they seem a little long, they are not complicated. If you follow them one by one, you should have your system up and running in no time.
Step 1: Installing Perl or Python
I recommend installing ActiveState Python or Perl. Recently, I discovered IronPython, but I do not know if IronPython works with CxxTest or not. After installation, make sure that Python or Perl is in your path.
Step 2: Installing CxxTest
Download CxxTest from its home page. I recommend to unzip CxxTest to the root directory, C:\, but you can pick other locations. After unzipping, add your directory to the PATH and to the INCLUDE environment variables. Here is how to do it in Vista:
- Click Start menu, click “Control Panel” from the right column to start the control panel
- From the control panel’s search box, type ‘env’ and you will see the choice for “Edit environment variables for you account”, click it.
- If the environment PATH does not exist, create it and assign the value C:\CxxTest (or the location of your CxxTest directory). If the PATH evironment already exist, append ;C:\CxxTest to it (note the semicolon which acts as a separator).
- Similarly, add C:\CxxTest to the INCLUDE variable
- Click OK to submit your changes
Step 3: Create a Test Project
This step is for those who is using Visual Studio. From Visual Studio, create a new project for your test, in my case, I created an empty project and add to it a new file call ‘mytestsuite.h’. This file is where my test cases reside. Please follow the CxxTest’s User Guide for information on how to create your test cases.
I also create a new file call ‘prebuild.cmd’ and add it to the project. The prebuild.cmd file contains only one line:
cxxtestgen –error-print -o mytestsuite.cxx mytestsuite.h
Basically, this script creates the code for registering the test cases. Next, you will configure Visual Studio to run this script before every build:
- In Visual Studio, Click the Project menu, then click Properties
- In the properties dialog, navigate to the following branch: Configuration Properties > Build Events > Pre-Build Event
- Fill in the following information:
- Command Line = prebuild.cmd
- Description = Build mytestsuite.cxx
- Excluded from build = No
- Click OK to submit your changes
- Run the build once to create the file mytestsuite.cxx
- Add this file to your project
Now you have everything setup, each time you build your test project, Visual Studio will launch prebuild.cmd and you will not have to think about it any more, just concentrate on writing your test cases. If you have any question, comment, or correction, please submit them via this blog’s comment.