C++ - Google Test - Set up projects to use the gtest library

Creating a library for your Google Test framework is a good practise.

It separates your main project, your library and your tests code.

A great way to deal with your dev team and your test team.

Visual Studio will be our IDE for this tutorial.

So let's get started.

First of all

For this tutorial we need:

Google Test will be installed in this directory:

  • C:\dev\c++\mylib\googletest-release-1.8.0

And before diving in the heart of this tutorial, it's important to have the gtestd.lib generated (the debug version with a "d").

If you don't know how to do this, follow this tutorial to generate the gtest.lib from gtest.sln with Visual Studio

Once done, come back here.

We are going to create 3 projects:

  1. BadprogMain
  2. BadprogStaticLibrary
  3. BadprogTests

The BadprogMain project will have only the main() function with one header from the static library.

The BadprogStaticLibrary project will have all our .cpp and .h files from where our BadprogMain will include a header.

And the BadprogTests project will be our testing project that will include the header from the BadprogStaticLibrary as well.

Project 1: BadprogMain

Create a new project from scratch with a new solution.

For that from Visual Studio > File > New > Project... > Win32 Console Application.

Or for the new Visual Studio version: Visual Studio > File > New > Project... > Installed > Windows Desktop > Windows Desktop Wizard.

With the following description:

  • Name: BadprogMain
  • Location: C:\dev\c++\vs\ 
  • Solution: Solution_3

Be sure to check Create directory for solution.

Then click OK > Next.

For the Application Settings we have:

  • Application type: Console application
  • Additional options: All unchecked

Click Finish.

Project 2: BadprogStaticLibrary

From the solution that we have just created, right-click Solution_3 then > Add > New Project... > Visual C++ > Win32 Console Application.

Or for the new Visual Studio version: Visual Studio > File > New > Project... > Installed > Windows Desktop > Windows Desktop Wizard.

With the following parameter:

  • Name: BadprogStaticLibrary
  • Location: C:\dev\c++\vs\Solution_3

Then click OK > Next.

For the Application Settings we have:

  • Application type: Static library
  • Additional options: All unchecked

Click Finish.

Project 3: BadprogTests

From the solution that we have just created, right-click Solution_3 then > Add > New Project... > Visual C++ > Win32 Console Application.

Or for the new Visual Studio version: Visual Studio > File > New > Project... > Installed > Windows Desktop > Windows Desktop Wizard.

With the following parameter:

  • Name: BadprogTests
  • Location: C:\dev\c++\vs\Solution_3

Then click OK > Next.

For the Application Settings we have:

  • Application type: Console application
  • Additional options: All unchecked

Click Finish.

BadprogMain

Open the BadprogMain.cpp file and copy the following code in order to have this:

// Badprog.com 
// BadprogMain/BadprogMain.cpp

#include "../BadprogStaticLibrary/BadprogCalculation.h"

int main()
{
    BadprogCalculation start;
    
    return 0;
}

As you can see, the code is straightforward.

We want, indeed, keep the main() function as simple as possible.

BadprogStaticLibrary

From the Solution Explorer, right-click the BadprogStaticLibrary project > Add > Class... > Add.

With the following settings:

  • Class name: BadprogCalculation
  • .h file: BadprogCalculation.h
  • .cpp file: BadprogCalculation.cpp
  • Base class: let it empty.
  • Access: public

Click Finish.

BadprogCalculation.h

Within the header let's type the following:

// Badprog.com 
// BadprogStaticLibrary/BadprogCalculation.h

#pragma once

class BadprogCalculation
{
public:
    BadprogCalculation();
    ~BadprogCalculation();

    int add(const int first, const int second);
    int substract(const int first, const int second);
    int multiply(const int first, const int second);
    int divide(const int first, const int second);
};

#pragma once

BadprogCalculation.cpp

In the C++ file, type this code:

// Badprog.com
// BadprogStaticLibrary/BadprogCalculation.cpp

#include <iostream>
#include "BadprogCalculation.h"

using namespace std;

/**
* Constructor
*/
BadprogCalculation::BadprogCalculation() {
    cout << "Hello from BadproG :D" << endl;
}

/**
* Destructor
*/
BadprogCalculation::~BadprogCalculation() {

}

/**
* Adding the first parameter with the second.
*/
int BadprogCalculation::add(const int first, const int second) {
    return (first + second);
}

/**
* Substracting the first parameter with the second.
*/
int BadprogCalculation::substract(const int first, const int second) {
    return (first - second);
}

/**
* Multiplying the first parameter by the second.
*/
int BadprogCalculation::multiply(const int first, const int second) {
    return (first * second);
}

/**
* Dividing the first parameter by the second.
*/
int BadprogCalculation::divide(const int first, const int second) {
    return (first / second);
}

BadprogTests

In the BadprogTests.cpp file, type down the following snippet:

// Badprog.com
// BadprogTests/BadprogTests.cpp

#include <gtest\gtest.h>
#include "../BadprogStaticLibrary/BadprogCalculation.h"

// adding
TEST(Test_Calculation, Adding) {
    BadprogCalculation calculation;
    EXPECT_EQ(5, calculation.add(2, 3));
}

// substracting
TEST(Test_Calculation, Substracting) {
    BadprogCalculation calculation;
    EXPECT_EQ(-1, calculation.substract(2, 3));
}

// multiplying
TEST(Test_Calculation, Multiplying) {
    BadprogCalculation calculation;
    EXPECT_EQ(6, calculation.multiply(2, 3));
}

// dividing
TEST(Test_Calculation, Dividing) {
    BadprogCalculation calculation;
    EXPECT_EQ(2, calculation.divide(10, 5));
}

// main
int main(int argc, char* argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
 

Setting the projects

Now that we have the code of each file, we have to set the projects to tell Visual Studio where to find libraries and files.

We will also change the code generation to have the same for the 3 projects (and the Google Test library).

And to be sure to have exactly the same configuration, we will choose the Debug mode, in your Configuration Manager, before building the solution.

Before that, be sure to have the Debug build set in the Configuration Manager.

From Visual Studio > Build > Configuration Manager... > Active solution configuration > Debug > Close.

BadprogMain

Let's add the library.

Right-click BadprogMain project > Properties > Configuration Properties > Linker > Input > Additional Dependencies > <Edit...>.

In the text area, add a new line: 

  • BadprogStaticLibrary.lib

Click OK > Apply > OK.

Let's add the directory.

Right-click BadprogMain project > Properties > Configuration Properties > Linker > General > Additional Library Directories > <Edit...>.

Add this line: 

  • C:\dev\c++\vs\Solution_3\Debug

Let's change the code generation.

Right-click BadprogMain project > Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug (/MTd).

Click OK > Apply >  OK.

It's important to specify that our BadprogMain project needs a reference, in this case the BadprogStaticLibrary.

It will, indeed, tell Visual Studio that we need this reference before building the current project.

Right-click BadprogMain project > Add > Reference... > Select BadprogStaticLibrary in the list > OK.

BadprogStaticLibrary

Here let's change only the code generation mode.

Right-click BadprogStaticLibray > Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug (/MTd).

Click OK > Apply >  OK.

BadprogTests

Let's start by set this project as setup.

Right-click BadprogTests project > Set as SetUp Project.

For this project we have exactly the same settings as Main because we use the same static library.

But we've also to add the Google Test library (the debug version for our example).

Let's add the libraries.

Right-click BadprogTests project > Properties > Configuration Properties > Linker > Input > Additional Dependencies > <Edit...>.

In the text area, add these 2 lines: 

  1. BadprogStaticLibrary.lib
  2. gtestd.lib (it's the debug version with a "d")

Click OK > Apply > OK.

Let's add the directories.

Right-click BadprogTests project > Properties > Configuration Properties > Linker > General > Additional Library Directories > <Edit...>.

Add these 2 lines: 

  1. C:\dev\c++\vs\Solution_3\Debug
  2. C:\dev\c++\mylib\googletest-release-1.8.0\googletest\msvc\gtest\Debug

Click OK > Apply > OK.

Let's add now some information to find the Google Test headers:

Right-click BadprogTests project > Properties > Configuration Properties > C/C++ > General > Additional Include Directories > <Edit...>.

Add the line: 

  • C:\dev\c++\mylib\googletest-release-1.8.0\googletest\include

Click OK > Apply > OK.

And change the code generation mode:

Right-click BadprogTest project > Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug (/MTd).

Click OK > Apply > OK.

Building the solution

It's now time to build the solution.

Right-click the Solution > Rebuild Solution.

Everything should be OK and something like that be in your Ouput console:

1>------ Rebuild All started: Project: BadprogStaticLibrary, Configuration: Debug Win32 ------
2>------ Rebuild All started: Project: BadprogTests, Configuration: Debug Win32 ------
1>BadprogCalculation.cpp
2>stdafx.cpp
2>BadprogTests.cpp
1>BadprogStaticLibrary.vcxproj -> C:\dev\c++\vs\Solution_3\Debug\BadprogStaticLibrary.lib
3>------ Rebuild All started: Project: BadprogMain, Configuration: Debug Win32 ------
3>stdafx.cpp
3>BadprogMain.cpp
3>Generating Code...
2>Generating Code...
3>BadprogMain.vcxproj -> C:\dev\c++\vs\Solution_3\Debug\BadprogMain.exe
3>BadprogMain.vcxproj -> C:\dev\c++\vs\Solution_3\Debug\BadprogMain.pdb (Partial PDB)
2>BadprogTests.vcxproj -> C:\dev\c++\vs\Solution_3\Debug\BadprogTests.exe
2>BadprogTests.vcxproj -> C:\dev\c++\vs\Solution_3\Debug\BadprogTests.pdb (Partial PDB)
========== Rebuild All: 3 succeeded, 0 failed, 0 skipped ==========

Running tests

Here we have 2 options:

  • Using the debug console
  • Using the Google Test Adapter

Debug console

From Visual Studio > Debug > Start Without Debugging.

You should see this kind of display:

[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from Test_Calculation
[ RUN      ] Test_Calculation.Adding
Hello from BadproG! :D
[       OK ] Test_Calculation.Adding (2 ms)
[ RUN      ] Test_Calculation.Substracting
Hello from BadproG! :D
[       OK ] Test_Calculation.Substracting (2 ms)
[ RUN      ] Test_Calculation.Multiplying
Hello from BadproG! :D
[       OK ] Test_Calculation.Multiplying (2 ms)
[ RUN      ] Test_Calculation.Dividing
Hello from BadproG! :D
[       OK ] Test_Calculation.Dividing (2 ms)
[----------] 4 tests from Test_Calculation (10 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (13 ms total)
[  PASSED  ] 4 tests.
Press any key to continue . . .

Google Test Adapter

If you prefer using this adapter, follow the last part of this Google Test tutorial for an explanation how to set it up.

Conclusion

This tutorial was really interesting because we learnt how to create a static library in order to be used by the main project and the tests project.

What is a good way to split projects correctly.

Furthermore we saw how to set Visual Studio for running our tests.

So congratulations if you read until this point, you did it, good job! smiley

Comments

Comment: 

Hi there, just to let you know you might want to edit the bit where you change the code generation mode for the static library. Where it says:

BadprogStaticLibrary
Here let's change only the code generation mode.
Right-click BadprogTets project > Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug (/MTd).

It should say:

BadprogStaticLibrary
Here let's change only the code generation mode.
Right-click BadprogStaticLibrary project > Properties > Configuration Properties > C/C++ > Code Generation > Runtime Library > Multi-threaded Debug (/MTd).

I know this was me being stupid and I should've picked up on the error, but I just assumed you meant BadprogTests and it wound up taking me a long time to figure out why my code wasn't working.

Also, you need to add '#include "stdafx.h"' to the BadprogCalculation.cpp source file.

Cheers, and thanks for the otherwise extremely helpful walkthrough!

Comment: 

Hello Frasher,

Thank you for your comment.

You are totally right and I changed it.

laugh

Comment: 

I have got an link error:(MSVC2015)
Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "bool __cdecl testing::internal::IsTrue(bool)" (?IsTrue@internal@testing@@YA_N_N@Z) referenced in function "public: void __thiscall testing::internal::scoped_ptr<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >::reset(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > *)" (?reset@?$scoped_ptr@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@internal@testing@@QAEXPAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) BadprogTests C:\dev\c++\vs\BadprogMain\BadprogTests\BadprogTests.obj 1

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.