This article focuses on the method of unit testing code in Visual Studio 2013, which contains two things: unit testing of existing DLL files, and unit testing of existing source files.
1.
VS2013 to
unit tests for DLL files
Unit test the existing DLL file.
- First, create a new DLL project to test Calculationdll, which contains 3 files: CalculationDll.cpp, CalculationDll.h, Calculationdll.def.
- Then, a new unit test project, Calculationtest, was created.
- Subsequently, the Calculationtest project properties include directories contains the directory of the DLL project Calculationdll to be tested.
- Subsequently, a reference to the test DLL project Calculationdll was added in the project properties of Calculationtest Common Properties > References
- Then, include CalculationDll.h in the Calculationtest and write the test case Test_method
- Finally, buildcalculationtest the project, open Test Exploerer, run the testing case
[1] Creating and writing a DLL project to be tested –calculationdll
New "Win32 project", named "Calculationdll", Solution named "Testexample", type selected as DLL, empty project.
New CPP File
Create a new. h file
New DEF file
Last Build the project
[2] Create test project –calculationtest
Add a new test project calculationtest in the current solution.
[3] include directory with header files
Calculationtest Project Properties > VC + + Directories > Include directories add calculationdll directories
[4] adding project references
Calculationtest Project Properties >common properties > References > Add New References > Tick calculationdll.
[5] Writing test code
Write the test code in Calculationtest's Unittest1.cpp, first containing the header file, and then writing two test methods.
[6] Build, run test
Build the Calculationdll project first, then select the menu "Test" > "Windows" > "Test Explorer" to open the Test Explorer window
You can run test cases in this window
2.
in VS2013
unit tests on existing source files
Unit test the source code in an existing C + + project.
- First of all, create a new project to be tested calculation, which contains 2 files: Calculation.cpp, Calculation.h.
- Then, a new unit test project, Myunittest, was created.
- Then, in the Myunittest project properties include directories, the directory for the project calculation to be tested is included
- Subsequently, the source code in the calculation project to be tested is added to the myunittest
- Then, include Calculation.h in the Myunittest and write the test case Test_method
- Finally, buildcalculationtest the project, open Test Exploerer, run the testing case
"1" new project to be tested –calculation
Create a new project with the Win32 Console application type named "Calculation" and solution named "Testexample". and add two simple functions add and sub to implement the Add and subtract operation.
For subsequent testing, the _tmain function is commented out first.
"2" Add header file –calculation.h
In order for the Add and sub functions to be tested by the test project, you need to create a new header file that contains the declarations of the two functions in the header file. This allows the test project to include the header file to use the Add and sub functions.
, the header file is "Calculation.h"
Include the header file in the Calculation.cpp
"3" New test project –myunittest
In Solution Explorer, right-Solution name "Testexample", select "Add" > "New Project", New project Type "Test", template Select "Native Unit Test Project", Named "Myunittest"
"4" contains directory
Add a directory for the project calculation to be tested in VC + + Directories > Include directories of the Myunittest project properties.
"5" adds an existing source code to the test project
Right-click the "Myunittest" project and select "Add" > "Existing Item" to add Calculation.h and calculation.cpp to the project
"7" Writing tests
Include Calculation.h in the unittest1.cpp of the Myunittest project, and then write two test methods Testadd and TestSub
"8" Build myunittest Project
You must build myunittest before you can see the test cases written in the test explorer in the next step
"0" Running test Cases in Test Explorer
The menu "Test" > "Windows" > "Test Explorer" opens the test explorer.
Run the test case here.
This article mainly explains the whole test process, and the following will supplement the specific unit test method.
Using C + + unit tests in Visual Studio 2013