在ObjectARX中进行单元测试
<div id="blogDetailDiv" style="FONT-SIZE: 16px;"><p>Unit Testing C++ AutoCAD ObjectARX</p><p>from:http://www.jlhdevelopment.com/wordpress/2008/12/22/unit-testing-c-autocad-objectarx/</p><p>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. <a href="http://www.junit.org/" target="_blank">JUnit</a> and <a href="http://www.nunit.org/index.php" target="_blank">NUnit</a> 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 <a href="http://gamesfromwithin.com/?p=29" target="_blank">this one</a> 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.</p><p>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 <a href="http://usa.autodesk.com/adsk/servlet/index?id=773204&siteID=123112" target="_blank">here is a good starting point</a> for doing that. So, here goes:</p><ol><li>Install the Boost libraries, usually you would download the source and compile these yourself, but thankfully <a href="http://www.boostpro.com/" target="_blank">BoostPro</a> has provided binaries for the VS family of compilers <a href="http://www.boostpro.com/products/free" target="_blank">here</a>. 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. </li><li>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. </li><li>Open the Property Pages for your new configuration. </li><li>Under Configuration Properties->General, change Configuration Type to Application (.exe) </li><li>Under C/C++->General, add C:\boost\boost_1_36_0 or wherever you installed the Boost libraries to Additional Include Directories. </li><li>Under C/C++->Code Generation, make sure Basic Runtime Checks is set to Default. </li><li>Under C/C++->Output Files, change the Output Files paths to .\UnitTest/ wherever you see .\Release/ </li><li>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. </li><li>Under Linker->System, change SubSystem from /SUBSYSTEM:Windows to /SUBSYSTEM:Console </li><li>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. </li><li>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”<br/>#define BOOST_TEST_MODULE TestYourProjectName<br/>#include <boost/test/included/unit_test.hpp> </li><li>Now, for each class YourClass that you want to write tests for, create a corresponding YourClassTest.cpp similar to the following:#include “stdafx.h”<br/>#include “YourClass.h” <p></p><p>#include <boost/test/unit_test.hpp></p><p>BOOST_AUTO_TEST_SUITE(name_of_class_to_test_test);</p><p>BOOST_AUTO_TEST_CASE(my_first_test_case)<br/>{<br/>//….some initialization code here<br/>BOOST_CHECK_EQUAL( 2==2, true);<br/>}</p><p>BOOST_AUTO_TEST_SUITE_END(); </p></li><li>Build with the new UnitTest configuration selected </li><li>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 </li><li>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. </li><li>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 <a href="http://www.boost.org/doc/" target="_blank">the Boost Documentation</a> for a more detailed explanation of the framework.</li></ol></div>
页:
[1]