In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use CppUnit for unit testing in C++". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use CppUnit for unit testing in C++".
If you use VC6, open the VC project file directly with VC6, select Batch Build (* reset the project parameters yourself), and then you can get the library files we need in the source code package subdirectory Lib. If you are using C _ VC8, open the VC6 project file with CAccord and follow the prompts to convert directly to the. net version.
Similarly, select batch build to generate the library files we need. When compiling source code in VC8, there will be a compilation error "# import" libid:80cc9f66-e7d8-4ddd-85b6-d9e6cd0e93e2 "version (" 7.0") lcid ("0") raw_interfaces_only named_guids ". Just change version (" 7.0") to version ("8.0").
In the process of developing an application, we may use many third-party libraries, such as CppUnit here. For convenience, we can create a dedicated folder for third-party libraries and create subfolders for specific libraries (such as CppUnit here). Then set the header file path and the library file connection path of the library in the Option of VC.
If there is a Dll file, add the directory where the DLL file is located in the system variable PATH, so that the program can be searched for the PATH + when the program is running (note that the PATH setting will not take effect for programs that run directly from VC until VC is restarted).
Similarly, here we add the header file and library file path of CppUnit to the default search path of VC. In addition, it should be pointed out that, in general, the Dll file needs to be released with the generated program, and at this point you must copy the Dll file to the Project directory. At this point, you can either do it manually or add a command to the Post-Build Step of Project to perform the copy action.
When using CppUnit for unit testing in VC, you can choose the console test of CppUnit as the interface and graphical test interface. The console interface is simple, while the graphical interface is more intuitive, and with the help of plug-ins provided by CppUnit, you can quickly locate the place where the assertion failed. Normally.
We use the console test interface to test the Console program and the graphical test interface to test the GUI program, so that the test Project does not affect the Project being tested. Because of the ease of use of the graphical test interface, we may also want to use it to test Console programs, which is a little more troublesome at this time. These uses are described below (take VC6 as an example, VC8 is similar).
In the following description, the tested Project is called the target Project and the tested class is called the target class. Test the Console program using CppUnit's Console test interface
1. Create a workspace for development and testing, and then create two Project in that workspace, one for development and the other for testing.
two。 In the test Project, the settings are as follows:
(1) set up connection libraries: cppunit.lib (Release version) and cppunitd.lib (Debug version), or use dynamic link library versions cppunit_dll.lib, cppunit_dll.dll and cppunitd_dll.lib, cppunitd_dll.dll
(2) set Use run-time library to Multithreaded Dll (Release version) and Debug Multithreaded Dll (Debug version) in the Code Generation of CCompact +.
3. Add a test class to the test Project. If the target class being tested is Demo, you can define a test class for DemoTest:
Class DemoTest: public CppUnit::TestFixture {CPPUNIT_TEST_SUITE (DemoTest); / / the parameter is the test class name CPPUNIT_TEST (testFunc); / / the parameter is the test function name, which corresponds to the test function declared below () Public: void setUp () {/ / initialize the test here} void tearDown () {/ / clean up after the test} void testFunc (); / / the test function is used to test some detail of the target class} # endif / / DEMO_TEST_H file DemoTest.cpp: # include "DemoTest.h" # include ".. / Demo/DemoTest.h" / / here is the header file path of the target class to be tested, because the Demo class # include CPPUNIT_TEST_SUITE_REGISTRATION (DemoTest) is used in the test Void DemoTest::testFunc () {/ / here to test the Demo class, you can use CPPUNIT_ASSERT, CPPUNIT_ASSERT_EQUAL, etc.} Thank you for reading. This is the content of "how to use CppUnit for unit testing in C++". After the study of this article, I believe you have a deeper understanding of the problem of how to use CppUnit for unit testing in C++. The specific use situation still needs to be verified by practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.