(Originally published at http://morison.biz/technotes/articles/23 on Sep. 1, 2007)
The CxxTest docs give a cumbersome, multi-project-with-makefile procedure for using CxxTest in Visual Studio. Here's something better
The CxxTest User Guide suggests one way of intergrating CxxTests with the visual studio IDE involving multiple projects per test suite. This article covers an alternative, single project approach.
The setup was done with Visual Studio 2005 Express. Your mileage may vary on other versions.
- Create a new win32 console project, e.g., TestCxx, in the solution. Make it an empty project with no precompiled header.
- Add a new header file, e.g., TestTest.h, to the Header Files of the project.
- Open the new .h file and add some CxxTests, e.g.,
class MyTestSuite : public CxxTest::TestSuite
void testMultiplication( void )
TS_ASSERT_EQUALS( 2 * 2, 5 );
- Right click on the .h file and select the Property page.
- In the property page go to the Custom Build Step section. Select the Command Line entry and add the appropriate cxxtestgen line, such as
python path/to/cxxtest/cxxtestgen.py --runner=ParenPrinter -o TestTest.cpp TestTest.h
- Adjust as needed for your python/perl and cxxtest setup. Be sure to use the "--runner=ParenPrinter" option, as this will produce messages that are clickable in the output window
- Put your .cpp filename (from the -o in cxxtestgen) in the Outputs list. For extra credit, add something pithy to the Description line, like "cxxtestgen".
- Go ahead and build your cxxtest project. This will generate a .cpp.
- Right click on Source Files and add the new .cpp file
- Go to the Project Properties page, find the Linker section, and copy the "Output" line (this should be where the project .exe file ends up).
- While still in the Project Properties page, find the Post Build Event section under Build Events. Past the .exe line copied in the previous step in the Command Line field. Again, for extra credit, add another witty description of what's going on.
- Also on the Project Properties pages: make sure there's an include path to the cxxtest headers. E.g., add path/to/cxxtest/ to Additional Include Directories, as well as any other build settings that your testing code requires.
- Go ahead and build your project. Not only will cxxtestgen get run (if needed), after the .exe is built the test cases will run. Errors from the test run streamed to the output window are double clickable to take you to the error assert line.
Gail on Sep. 4 2008
This article describes exactly what I am trying to do. The problem is that in the post-build step, the tests do not appear to run inside VS.NET 2003. Not only that, I cannot get any output from the tests to appear in the IDE output window whether or not the test fails. The pre-build works fine and it generates an executable that works properly when run in a cmd shell - that is the output from the cxxtest suite is shown properly.
I wrote two simple tests one of which fails: TS_FAIL("ERROR"). The other always passes: TS_ASSERT(1 == 1);
My pre-build command is: cxxtestgen.pl -o MyTest.cpp --runner=ParenPrinter MyTest.h
My post-build command is 3 lines: echo "Run test"
echo "Test Completed"
The echoes work fine. Thank you Microsoft
If you have any ideas I would be grateful for the help! Thanks.
Kfitch on Sep. 8 2008
Gail on Sep. 19 2008
I finally resolved the issues I had when trying to call runner.exe within the Visual Studio 2003 environment using a Post-Build Event. No output would show in the Build window (runner.exe was built using --runner=ParenPrinter).
The problem arose because my runner.exe depends on some external DLLs. The WINDOWS DLL runtime search order is: the parent directory of the executable, some system directories, and then the directories listed in %PATH%. I did not realize that Visual Studio does NOT use environment variables, like %PATH%, added or modified after VS has been installed!!!! It should be noted however that when running your program in the Debugger from within the IDE, it DOES use updates to the environment in place when the IDE is started. Even if you start devenv.exe from a shell, as opposed to a shortcut, the Post-Build event has no knowledge of changes to your environment that were made after installation. Only the geniuses at Microsoft can explain this behavior.
What this means is that external DLLs that are needed to run your test, and which you added to your PATH variable, are not available when the Post-Build event executes.
There are 2 solutions. The better option is to add the necessary env vars and update the path in a batch file that calls runner.exe. For example, place the following in a postbuild.cmd file located with your source code and call it in the Post-Build event:
set QTDIR=c:\someplace\qt\WINDOWS path = %PATH%;%QTDIR%\debug\lib runner.exe
The other solution is to add an entry to the VS Tools->Options->Projects->C++ Directories dialog when the the "Show Directories for:" dialog says "Executable Files". To match the above batch script, the entry would be c:\someplace\qt\WINDOWS\debug\lib.
I prefer the former since what is going on visible and explicit instead of being hidden in one of the dozens of dialogs in the IDE.
Kupps on Nov. 12 2009
Good setup! Mine works fine until I add a second header file to the project...
Should I be changing the settings on the second header file's Custom Build Step...?