Unit Testing C++ AutoCAD ObjectARX
from:http://www.jlhdevelopment.com/wordpress/2008/12/22/unit-testing-c-autocad-objectarx/
Let me start by saying that coming from a Java/C# development background, I found unit testing in C++ to be a real pain in the arse. JUnit and NUnit are the de facto unit testing frameworks for Java and .NET development. They are simple, easy to use, well documented, and have a decent community around them in case you do get stuck. Unfortunately, a de facto C++ unit testing framework does not exist, and there are so many options that articles such as this one exist that compare and contrast some of the more popular frameworks and attempt to point you in the right direction. However, after hours of digging through documentation and playing with various frameworks, I almost gave up trying to get one that would compile and link against an ObjectARX application.
This is a very short overview of how we’ve managed to get unit testing setup for our ObjectARX C++ application. I want to point out from the onset that this is *hopefully* not the easiest or cleanest way to unit test an ObjectARX application, but it does work and is relatively straightforward. I am using Visual Studio 2005 and dynamic linking, your mileage may vary with different compilers and linking options. I’m also assuming you have successfully compiled and tested your ObjectARX application, but if not here is a good starting point for doing that. So, here goes:
- Install the Boost libraries, usually you would download the source and compile these yourself, but thankfully BoostPro has provided binaries for the VS family of compilers here. Run the installer to install the appropriate binaries on your system. I selected all of the types of libraries because the Multithread DLL option gave me linker errors. If you want to try selecting only the ones you need, good luck.
- Create a new configuration, select Build->Configuaration Manager, then select from the dropdown on the top left. Name it UnitTest or whatever you want, and copy the settings from the Release configuration.
- Open the Property Pages for your new configuration.
- Under Configuration Properties->General, change Configuration Type to Application (.exe)
- Under C/C++->General, add C:\boost\boost_1_36_0 or wherever you installed the Boost libraries to Additional Include Directories.
- Under C/C++->Code Generation, make sure Basic Runtime Checks is set to Default.
- Under C/C++->Output Files, change the Output Files paths to .\UnitTest/ wherever you see .\Release/
- Under Linker->General, change the output file from your arx file to TestYourProject.exe, where YourProject is the name of your ObjectARX application. Also add C:\boost\boost_1_36_0\lib to Additional Library Directories.
- Under Linker->System, change SubSystem from /SUBSYSTEM:Windows to /SUBSYSTEM:Console
- Now hit apply, hit OK to close the window, and build the new configuration and make sure you don’t get any compiler or linker errors.
- Create a new .cpp file, name it Test.cpp or something similar. This will create an entry point for the unit tests. In the body, put this:#include “stdafx.h”
#define BOOST_TEST_MODULE TestYourProjectName
#include <boost/test/included/unit_test.hpp> - Now, for each class YourClass that you want to write tests for, create a corresponding YourClassTest.cpp similar to the following:#include “stdafx.h”
#include “YourClass.h” #include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_SUITE(name_of_class_to_test_test);
BOOST_AUTO_TEST_CASE(my_first_test_case)
{
//….some initialization code here
BOOST_CHECK_EQUAL( 2==2, true);
}
BOOST_AUTO_TEST_SUITE_END();
- Build with the new UnitTest configuration selected
- Add the AutoCAD installation to your path, in my case it is: C:\Program Files\AutoCAD 2007, so that Windows will find the dll’s
- Open a command shell and run the TestYourProject.exe from the command line. If successful, you should see the following: “Running x test cases….” followed by the result of the tests.
- That’s it, you should now have a way to unit test your ObjectARX applications using the Boost Test library. You also probably want to use a preprocessor command and ifdef all of your *Test classes so that they don’t get included in your production build. Refer to the Boost Documentation for a more detailed explanation of the framework.