Set Up GTest for TDD. C++, VS

Published March 31, 2019
Advertisement

Before I will start I want to advise you to read this book: The Art of Unit Testing: with examples in C#. Yes, as you can see this book contains examples in C# but it is not important. This book contain very useful and important information how to write unit tests.

I made an example of project in VS 2015: SortFunctions.zip  This project will show you how set up Google Test in Visual Studio and run simple unit tests.

Note. If you have another version of VS then before you will run unit tests you need to select VS 2017, like in this screenshot:

Spoiler

PlatformToolset.png.3c9a59798e0e30baeee97b337b6bbe48.png

Google Test library is included in the project as source folder and it is placed in "Libs" folder. You need to:

  • open the solution. The solution is file with name: "SortFunctions.sln"
  • select your version of VS, for example VS 2017 instead of VS 2015 as in screenshot above
  • make the "SortFunction_UnitTests" project as "StartUp Project". For this: make right mouse button click on the "SortFunction_UnitTests" project -> select "Set as StartUp Project"
  • press Ctrl+F5 to run unit tests

You will see this settings in the "SortFunction_UnitTests" project properties:


$(SolutionDir)Libs\gtest-1.8.1\include
$(SolutionDir)Libs\gtest-1.8.1
$(SolutionDir)SortFunction

This solution include two projects:

  • SortFunctions - this project contains modules that we want to test. For example, bubbleSort() method
  • SortFunctions_UnitTests - this project contains unit tests

Add existing files to the "SortFunctions_UnitTests" project:

  • YourSolutionDir/Libs/gtest-1.8.1/src/gtest-all.cc
  • YourSolutionDir/SortFunctions/SortFunctions.cpp

The "SortFunctions" project has two files:

SortFunctions.h


#pragma once
 
extern void bubbleSort(int *array, unsigned int amount);
 
extern void countingSort(int *array, unsigned int amount);

SortFunctions.cpp


#include "SortFunctions.h"
 
void bubbleSort(int *array, unsigned int amount)
{
 
}
 
void countingSort(int *array, unsigned int amount)
{
 
}

The "SortFunctions_UnitTests" project has tests. For example, this is the "bubbleSortTests.cpp" with two tests. The first test is for positive numbers and the second test is for negative numbers:

bubbleSortTests.cpp


#include <gtest/gtest.h>
 
#include "SortFunctions.h"
 
TEST(bubbleSortTest, AllPositiveElements)
{
    // Arrange
    const unsigned int amount = 5;
    int actualArray[amount] = { 5, 3, 10, 2, 7 };
    int expectedArray[amount] = { 2, 3, 5, 7, 10 };
 
    // Act
    bubbleSort(actualArray, amount);
 
    // Assert
    for (size_t i = 0; i < amount; i++)
    {
        ASSERT_EQ(expectedArray[i], actualArray[i]);
    }
}
 
TEST(bubbleSortTest, AllNegativeElements)
{
    // Arrange
    const unsigned int amount = 5;
    int actualArray[amount] = { -5, -3, -10, -2, -7 };
    int expectedArray[amount] = { -10, -7, -5, -3, -2 };
 
    // Act
    bubbleSort(actualArray, amount);
 
    // Assert
    for (size_t i = 0; i < amount; i++)
    {
        ASSERT_EQ(expectedArray[i], actualArray[i]);
    }
}

main.cpp


#include <gtest/gtest.h>

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

 

Previous Entry GameDev Books
1 likes 6 comments

Comments

fleabay

" I opened Setting of the project and add these lines to "C/C++" -> "General" -> "Additional Include Directories": "

It's a lot better creating .prop files for you projects under Property Manager. You can reuse them (put in a common folder) for every other project that uses similar libs/settings. Or you can copy and 'fork' them for a new project. It also relieves the frustration of not picking Release settings when in Debug or vice-versa (What a load of crap that is, right?). If interested, it's has a tab near the Solution Explorer pane tab, close to the bottom.

Oh and... Tests? Tests? We don't need no stinkin' tests. :D j/k

February 18, 2019 05:34 PM
8Observer8
February 19, 2019 12:17 AM
8Observer8
7 hours ago, fleabay said:

It's a lot better creating .prop files for you projects under Property Manager. You can reuse them (put in a common folder)

It useful for projects on my computer where each library has only on copy. But I include libraries in some projects because I need to public VS projects and everyone can open it and run them immediately. I make the "Libs" folder inside each such solution, put all necessary libraries to the "Libs" folder and connect them to the project using "$(SolutionDir)Libs\..." paths.

February 19, 2019 12:28 AM
8Observer8

I added this information:

Quote

Before I will start I want to advise you to read this book: The Art of Unit Testing: with examples in C#. Yes, as you can see this book contains examples in C# but it is not important. This book contain very useful and important information how to write unit tests.

 

March 31, 2019 09:02 AM
8Observer8

I added:

Add existing files to the "SortFunctions_UnitTests" project:

  • YourSolutionDir/Libs/gtest-1.8.1/src/gtest-all.cc
  • YourSolutionDir/SortFunctions/SortFunctions.cpp
March 31, 2019 11:06 AM
8Observer8

I add contents of the "main.cpp" for running the unit tests:

main.cpp


#include <gtest/gtest.h>

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

 

March 31, 2019 11:18 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Advertisement