In VS2013, you can use the VS self-contained C + + unit Test Framework.
Before you can use this framework, you need to install Unit Test Generator(which is available through the menu tools, extensions and updates search installation).
Below, explain the steps to use the framework:
1. Create a new empty Win32 console project Nativeunittest
Change the project's "Configuration Type" to "Dynamic Library (DLL)", such as:
2. New Unit Test Project
Under Solution Nativeunittest, create a new unit test project, "Visual C + + test-, native unit test project," as shown in:
3. Build Nativeunittest Project
The purpose of this is to generate the . obj file (in nativeunittest/debug/) so that the unit test project can be added.
4. Modifying Unit test Project Properties
The details are as follows:
5. Writing Unit Test files
The header and source files for the Nativeunittest project are as follows:
1 classTrivialcalculator2 {3 Public:4 DoubleADD (DoubleXDoubley);5 DoubleSubtract (DoubleXDoubley);6 DoubleMultiply (DoubleXDoubley);7 DoubleDivide (DoubleXDoubley);8};
TrivialCalculator.h
1#include"TrivialCalculator.h"2 3 DoubleTrivialcalculator::add (DoubleXDoubley)4 {5 returnX +y;6 }7 8 DoubleTrivialcalculator::subtract (DoubleXDoubley)9 {Ten returnX-y; One } A - DoubleTrivialcalculator::multiply (DoubleXDoubley) - { the returnX *y; - } - - DoubleTrivialcalculator::D ivide (DoubleXDoubley) + { - returnX/y; +}
TrivialCalculator.cpp
The unit test files are as follows:
1#include"stdafx.h"2#include"CppUnitTest.h"3#include".. \nativeunittest\trivialcalculator.h"4 5 using namespacemicrosoft::visualstudio::cppunittestframework;6 using namespacestd;7 8 namespaceNativeunittest_namespace9 {Ten Test_class (trivialcalculatortest) One { A Public: - trivialcalculatortest () - { theTest =NewTrivialcalculator; - } - -~trivialcalculatortest () + { - Deletetest; + } A at Test_method (addtest) - { - //trivialcalculator test; - Doubleactual = Test->add (1.0,2.0); - Doubleexpected =3.0; - inAssert::areequal (expected, actual,0.0001); - } to + Test_method (subtracttest) - { the //trivialcalculator test; * Doubleactual = Test->subtract (2.0,1.0); $ Doubleexpected =1.0;Panax Notoginseng -Assert::areequal (expected, actual,0.0001); the } + A the Public: +Trivialcalculator *test; - $ }; $ -}
The concrete steps to this end!
Project files can be downloaded from Baidu Cloud.
Resources:
Using Microsoft.VisualStudio.TestTools.CppUnitTestFramework
(English version using Microsoft.VisualStudio.TestTools.CppUnitTestFramework)
Writing Native C + + Unit Tests with VS Test
Unit testing native code with Test Explorer
Visual Studio Unit Test C + +-Test existing projects
Using C + + unit tests in Visual Studio 2013
How to use VS2013 local C + + unit Test framework