Visual studio 2019 C + + unit test framework cppunittest configuration description

Time:2021-7-30

Problem Description:
In the development process, we need to test the algorithm we write. Using debugging tools or running debugging will have problems such as inadequate testing and low testing efficiency. Using unit testing will be much better. It is efficient and easy to understand, and it is more in line with the idea based on TDD (Test Driven Development), Here, the configuration process of C + + unit test framework cppunittest under vs is recorded as follows.

Text content:
First, the unit test project will be installed by default in Visual Studio along with the installation of various language environments. For example, when the. Net development environment is installed, the test frameworks such as NUnit mstest will be installed. Here we briefly introduce the convenience that visual studio can provide us during C / C + + development.

  1. install
    When visual studio 2019 is installed for the first time, as long as “desktop development using C + +” is selected, Google test module and boost test module will be installed by default, as shown in the figure:
    Visual studio 2019 C + + unit test framework cppunittest configuration description
  2. First, we create an empty solution, and then create an empty project based on C + + under the empty solution. We name it “projforunittest”
    Visual studio 2019 C + + unit test framework cppunittest configuration description
    We simply create a C + + class and a function with a return value of int to explain the unit test. Here, we create a class called Calc, and then create and implement a function called add, which is used to add X and Y parameters and return the result.
  3. Create test project cppunittest
    Right click the solution – > Add – > new project, and enter test in the project template search bar. The following interface will appear
    Visual studio 2019 C + + unit test framework cppunittest configuration description
    We can see two unit test project templates marked with C + +, one is Google test and the other is native unit test project. The native unit test project comes from Microsoft, which is relatively simple. We select it here to create it. After creation, the contents are as follows
    Visual studio 2019 C + + unit test framework cppunittest configuration description
  4. Project settings
    (1) Change the configuration type of the test target project to dynamic library. DLL or static library. Lib
    Right click the test target project – > properties – > General – > configuration type – > static library (. LIB)
    (2) Change the output directory of both the test target project and the test project to projectdir (the default setting is solutiondir)
    Properties – > General – > output directory, replace solutiondir with projectdir
    (3) Add the test target project to the unit test project reference
    Select the “reference” node under the unit test project, right-click – > Add Reference – > select the test target project, and click OK
    (4) Add the directory of the header file exposed in the test target project to the additional include directory of the unit test project
    Select the unit test item, right click – > C / C + + – > General – > attach include directory – > Edit
    Visual studio 2019 C + + unit test framework cppunittest configuration description

Click the add directory button and select the directory where the header file of the test target directory is located.
(5) Add the output directory of the. Lib or. DLL file of the test target project to the additional library directory of the unit test file, and add. lib file name(Is the name of the test target project) to attach the dependent library.
1 add additional Library Directory
Right click the test item attribute – > linker – > General – > additional library directory (as described in Article 4 above) to add the output directory of Lib file to the list (generally, the debug directory under the project directory is used in the test)
2 add library files to attached dependent Libraries
Visual studio 2019 C + + unit test framework cppunittest configuration description
Right click Properties of test item – > linker – > Input – > attach dependency Library
Visual studio 2019 C + + unit test framework cppunittest configuration description
Enter in the text box that opens later.lib(Refers to the Lib file name output by the target test project.)
The above is the configuration process between C + + unit test projects.