Click here to Skip to main content
15,886,830 members
Articles / Programming Languages / C++14
Tip/Trick

How To Do Unit Testing with C++ in Visual Studio 2015

Rate me:
Please Sign up or sign in to vote.
4.13/5 (7 votes)
13 Mar 2016CPOL2 min read 42.5K   9   4
Let´s do unit testing in a C++ project inside Visual Studio 2015 quick and easy

Introduction

Using units test in a project with Visual Studio should be easy, and it is with C# but not with C++ projects, I have been trying for some time until I finally can make the way tests should be used so I think more people may have the same problem and might be interested to learn how to use it.

Background

We will have two projects, HelloWorld and Test. HelloWorld will contain the normal project with all we need and the Test project will be the one who contains the unit tests.

Using the Code

The first step is create a project on which we will have the source code for testing.

Creating the Project

Open Visual Studio -> New Project -> Visual C++ -> Win32 -> Console App

Image 1

I want the project to be as simple as possible, so I will stick with the default settings.

Image 2

Creating the Test Project

Next, we have to create another project which is really the Test project.

Click right the solution -> add -> new project

Image 3

I prefer to create the test project inside the project itself so I pick up, browse and put it inside the project.

Image 4

Next, we choose the "Native Unit Test Project".

Image 5

Changing the Order of the Build

We need to change the build order because the tests are going to use the compiled program that we are developing so it needs to be compiled before the tests runs.

In the solution -> Project dependencies:

Image 6

 

We pick the test project and tick the checkbox that contains the other project:

Image 7

Linking the Source Files into the Test Project

Be aware that any changes you make to any linked file will be reflected in both projects. If you delete from one project, you will delete from the other too so be careful because it is not a copy, it is a reference to a file.

Right click on the Test project -> Add -> Existing item

Image 8

We pick the headers and classes that we need, I created the header for HelloWorld so I can now link it.

Image 9

And I make a new folder for the source code for the project to make it clear which classes belong to what project. I didn't do with the headers because it is a test project and will no have headers, only classes.

Image 10

Source Code for the Files

It is time to put some source code inside the files, we will have two methods, one is static and the other is instantiated. There are three files:

  • HelloWorld.h: The header for HelloWorld:
    C++
    #pragma once
    
    class HelloWorld
    {
    	int n = 10;
    public:
    	static int getTwo();
    	int getN() const;
    };
  • HelloWorld.cpp: The source code for the class we want to test:
    C++
    #include "stdafx.h"
    #include "HelloWorld.h"
    
    int main()
    {
        return 0;
    }
    
    int HelloWorld::getTwo()
    {
    	return 2;
    }
    
    int HelloWorld::getN() const
    {
    	return n;
    }
  • UnitTest1: The class which has the unit tests:
    C++
    #include "stdafx.h"
    #include "CppUnitTest.h"
    #include "../HelloWorld.h"
    
    using namespace Microsoft::VisualStudio::CppUnitTestFramework;
    
    namespace UnitTest1
    {		
    	TEST_CLASS(UnitTest1)
    	{
    	public:
    		
    		TEST_METHOD(TestMethodGetTwo)
    		{
    			Assert::AreEqual(2, HelloWorld::getTwo());
    		}
    
    		TEST_METHOD(TestMethodGetN)
    		{
    			HelloWorld hw = HelloWorld();
    			Assert::AreNotEqual(0, hw.getN());
    		}
    	};
    }

It looks like that:

Image 11

And finally, we can run the tests:

Image 12

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThank You ! Pin
Sylvain Ballerini5-Jul-17 3:41
Sylvain Ballerini5-Jul-17 3:41 
QuestionSome pictures are not shown. Pin
oleg6316-Mar-16 5:03
professionaloleg6316-Mar-16 5:03 
QuestionNo, there are no missing images! Pin
Bleriot15-Mar-16 1:47
professionalBleriot15-Mar-16 1:47 
QuestionMissing images Pin
Bleriot15-Mar-16 1:44
professionalBleriot15-Mar-16 1:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.