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
I want the project to be as simple as possible, so I will stick with the default settings.
Creating the Test Project
Next, we have to create another project which is really the Test
project.
Click right the solution -> add -> new project
I prefer to create the test project inside the project itself so I pick up, browse and put it inside the project.
Next, we choose the "Native Unit Test Project".
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:
We pick the test project and tick the checkbox that contains the other project:
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
We pick the headers and classes that we need, I created the header for HelloWorld
so I can now link it.
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.
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
:
#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:
#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:
#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:
And finally, we can run the tests: