明经CAD社区

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 2785|回复: 0

在ObjectARX中进行单元测试

[复制链接]
发表于 2010-4-23 11:22:00 | 显示全部楼层 |阅读模式

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:

  1. 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.
  2. 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.
  3. Open the Property Pages for your new configuration.
  4. Under Configuration Properties->General, change Configuration Type to Application (.exe)
  5. Under C/C++->General, add C:\boost\boost_1_36_0 or wherever you installed the Boost libraries to Additional Include Directories.
  6. Under C/C++->Code Generation, make sure Basic Runtime Checks is set to Default.
  7. Under C/C++->Output Files, change the Output Files paths to .\UnitTest/ wherever you see .\Release/
  8. 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.
  9. Under Linker->System, change SubSystem from /SUBSYSTEM:Windows to /SUBSYSTEM:Console
  10. 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.
  11. 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>
  12. 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();

  13. Build with the new UnitTest configuration selected
  14. 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
  15. 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.
  16. 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.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|CAD论坛|CAD教程|CAD下载|联系我们|关于明经|明经通道 ( 粤ICP备05003914号 )  
©2000-2023 明经通道 版权所有 本站代码,在未取得本站及作者授权的情况下,不得用于商业用途

GMT+8, 2024-11-25 12:32 , Processed in 0.143863 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表