Visual Studio (VS) C + + unit tests

Source: Internet
Author: User
Tags assert

Copyright notice: If no source note, techie bright blog article are original. Reprint please indicate the title and address of this article in the form of link:
This article title: Visual Studio (VS) C + + unit Tests This article address: http://techieliang.com/2017/12/516/ Article Directory
    • 1. Create a new project to be tested MyProgram
    • 1.1. Create a new test project Myprogramtest
    • 1.2. The necessary configuration
    • 2. XXXtext.cpp Test File description
    • 3. Run unit Tests
    • 4. Other
    • 4.1. Parallel testing
    • 4.2. Code Coverage testing
1. Create a new project to be tested MyProgram

A new "Win32 console Application" was created, in which a new "my_math.h" file was created, in order to facilitate the creation of a class and. cpp file, using a simple function as an example.

    1. My_math.h
    2. #pragma once
    3. int Add(int A, int b) {
    4. return A + b;
    5. }
1.1. Create a new test project Myprogramtest

Here you select the Visual c++-> test----the Native unit test project, fill in the name, click OK, no additional configuration will be created under the current solution to create a new project.

Note that when you create a new project, you right-click the current solution-add-new item, which defaults to the current solution

The system generates four files "stdafx.h", "Stdafx.cpp" (Standard application Framework Extensions) precompiled header file by default, "targetver.h" Run environment definition header file, " Unittest1.cpp "test file. The first three no tube, directly see the fourth Test file can be.

1.2. The necessary configuration

After you create a new Myprogramtest project, add "in properties-connector-input-Additional dependencies". \myprogram\debug\*.obj "

It is recommended to use a relative path, using * to indicate all. obj suffix files. Note You only need to configure the unit test project, and you do not need to make any modifications to the original project.

Obj file (the Microsoft-introduced program compiles an intermediate code file), and the intermediate code file that is generated when the program compiles. The target file, typically a compiled binary file, is then made executable by linking the linker and the resource file. Obj only gives the relative address of the program, and the executable file is the absolute address.

2. XXXtext.cpp Test File description
  1. #include "stdafx.h"
  2. #include "CppUnitTest.h"
  3. #include ". /MYPROGRAM/MY_MATH.H "//Add header file for original project, suggest relative path
  4. Using namespace microsoft::visualstudio::cppunittestframework;
  5. Namespace myprogramtest {//myprogram Project Unit test
  6. test_class(UnitTest1) {//test class
  7. public:
  8. Test_method(TestMethod1) {//Test function
  9. //TODO: Enter the test code here
  10. Assert:AreEqual( add(5, ten));
  11. }
  12. };
  13. }

itself contains the original project was measured function header file "#include". /MYPROGRAM/MY_MATH.H "//Add header file for original project, suggest relative path"

UnitTest1 for test class name, Test_class for VS provides test class macro definition

    1. #define TEST_CLASS (className) \
    2. Only_used_at_namespace_scope class ClassName: public :: Microsoft::visualstudio:: cppunittestframework::testclass<classname>

TESTMETHOD1 is a test function name, Test_method is a test function macro definition for VS

Assert is an assertion class that provides multiple methods for AreEqual, Aresame, Arenotequal, Arenotsame, IsNull, Isnotnull, IsTrue, IsFalse, etc. to be asserted in the test

  1. Assert. inconclusive()//indicates an unauthenticated test;
  2. Assert. AreEqual() //Tests whether the specified values are equal and if equal, the test passes;
  3. Aresame() ///To verify that the specified two object variables are pointing to the same object, otherwise it is considered an error
  4. Arenotsame() ///To verify that the specified two object variable is pointing to a different object, otherwise it is considered an error
  5. Assert. IsTrue() //tests whether the specified condition is true and if true, the test passes;
  6. Assert. IsFalse() //tests whether the specified condition is false, and if false, the test passes;
  7. Assert. IsNull() //Tests whether the specified object is a null reference, and if it is empty, the test passes;
  8. Assert. isnotnull() //Tests whether the specified object is non-null, and if not NULL, the test passes;

If you need multiple test functions, just create multiple test_method under public:

If you need more than one test class, you can create a new CPP file and note that the VS unit test file is included CppUnitTest.h

3. Run unit Tests

Unit tests run without the need to compile the original program in advance and compile automatically when the test is run.

In the menu bar-test-run Select Run all tests

Selecting this item compiles the target project and then executes the test function in public in all test classes (you can temporarily change to private if you do not need to test).

The test results are displayed in the test explorer

If this window does not pop up automatically, you can open it in the menu bar-Test-window

4. Other 4.1. Parallel testing

The three bi-directional arrow buttons on the left side of the Test Explorer search box, click to enter the selected state, which opens the parallel test function.

4.2. Code Coverage testing

For VS2015 Enterprise Editions under the Test menu, "Analyze code Coverage" can take advantage of the current unit test to analyze code coverage for the original project.

VS2015 Community version does not analyze code coverage features

Visual Studio (VS) C + + unit tests

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.