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:
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();
}
" 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. j/k