Problem description: In the development process, we need to test the algorithm we write. There will be some problems such as inadequate testing and low testing efficiency when we use debugging tools or run it. Unit testing is much better because it is efficient, easy to understand, and more in line with the idea of TDD (Test Driven Development). The configuration process of the C++ unit testing framework CPPunittest under VS is documented as follows.

First of all, unit test projects are installed by default in Visual Studio with the installation of various locale, such as NUnit Mstest and other testing frameworks when the.NET development environment is installed. Here’s a quick look at some of the benefits Visual Studio can offer when developing C/C++.

  1. The Google Test and Boost Test modules will be installed by default if you select “Desktop Development with C++” during the first installation, as shown in the figure below:
  2. We will simply create a C++ class and create a function that returns an int for unit testing purposes. Here we create a class called Calc, and then create and implement a function called Add that adds the x and y arguments and returns the result.
  3. We can see two unit Test project templates in C++, one is Google Test, the other is native unit Test project, and the other is native unit Test project. The native unit test project is from Microsoft, which is relatively simple. We select it to create here. After the creation, the directory is as follows
  4. Project Settings (1) Change the configuration type of the test target project to either Dynamic Library.dll or Static Library.lib. Right click on the test target project -> Properties -> General -> Configuration Type -> Static Libraries (.lib) (2) Change the output directory of both the test target project and the test project to projectDir (default is solutionDir) -> general -> output directory, Replace SolutionDir with ProjectDir (3) Add the test target item to the unit test project reference select the Reference node under the unit test item right click -> Add Reference -> select the test target item and hit OK Add the directory where the headers are exposed in the test target project to the additional include directory of the unit test project. Select the unit test project right click ->C/C++-> General -> Additional include directory -> edit

Click the Add Directory button to select the directory where the header file of the test target directory is located.

(5) Add the output directory of.lib or.dll files of the test target project to the additional library directory of the unit test file, and add.lib filename (Is the name of the test target project) to the attached dependent library.

Add additional library directories

Test Project Right-click Properties -> Linker -> General -> Additional Library Directory (Ditto # 4 above) Add the output directory of the lib file to the list (Debug directory under the project directory is normally used for testing)

Add the library file to the attached dependent library

Test project right click properties -> linker -> input -> attached dependency library

Type in the text box that opens later.lib (The lib file name that refers to the output of the target test item.

This is the configuration process between C++ unit test projects.