• Breaking News

    Set Up GTest for TDD. C++, VS 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 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 #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]); } } https://ift.tt/eA8V8J

    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 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]); } }

    from GameDev.net https://ift.tt/2FLAMBN

    ليست هناك تعليقات

    Post Top Ad

    ad728

    Post Bottom Ad

    ad728