Форум программистов, компьютерный форум, киберфорум
Наши страницы
8Observer8
Войти
Регистрация
Восстановить пароль
Рейтинг: 4.50. Голосов: 2.

Set Up Google Test Framework for TDD. C++, Visual Studio

Запись от 8Observer8 размещена 13.02.2019 в 13:53
Обновил(-а) 8Observer8 31.03.2019 в 14:19

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:

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 the 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 includes 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

C++
1
2
3
4
5
#pragma once
 
extern void bubbleSort(int *array, unsigned int amount);
 
extern void countingSort(int *array, unsigned int amount);
SortFunctions.cpp

C++
1
2
3
4
5
6
7
8
9
10
11
#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

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#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]);
    }
}
I add contents of the "main.cpp" for running the unit tests:

main.cpp

C++
1
2
3
4
5
6
7
#include <gtest/gtest.h>
 
int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
Размещено в Без категории
Просмотров 217 Комментарии 3
Всего комментариев 3
Комментарии
  1. Старый комментарий
    Запись от 8Observer8 размещена 19.02.2019 в 02:49 8Observer8 вне форума
  2. Старый комментарий
    Аватар для 8Observer8
    I added:

    Цитата:
    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.
    Цитата:
    Add existing files to the "SortFunctions_UnitTests" project:
    • YourSolutionDir/Libs/gtest-1.8.1/src/gtest-all.cc
    • YourSolutionDir/SortFunctions/SortFunctions.cpp
    Запись от 8Observer8 размещена 31.03.2019 в 12:47 8Observer8 вне форума
    Обновил(-а) 8Observer8 31.03.2019 в 14:21
  3. Старый комментарий
    Аватар для 8Observer8
    I add contents of the "main.cpp" for running the unit tests:

    C++
    1
    2
    3
    4
    5
    6
    7
    
    #include <gtest/gtest.h>
     
    int main(int argc, char **argv)
    {
        ::testing::InitGoogleTest(&argc, argv);
        return RUN_ALL_TESTS();
    }
    Запись от 8Observer8 размещена 31.03.2019 в 14:20 8Observer8 вне форума
 
КиберФорум - форум программистов, компьютерный форум, программирование
Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Рейтинг@Mail.ru