Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++14

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

4.13/5 (7 votes)
13 Mar 2016CPOL2 min read 43.4K  
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)