Powered by
/src$ make
  • Home
  • About
  • Directory
  • Contact
  • Home
  • About
  • Directory
  • Contact

How To Install/Use Google C++ Test Framework

3/7/2018

 
To see the youtube video where I go over the installation/testing with you, please click here.

Introduction: Unit Tests

Testing is a necessary part of any program. Often, project's get rather large and there are many files of code with many classes and functions, all of which needs to work correctly. For this reason, we write Unit Tests to test each file, function, or class in our project. (There's also testing for the application/server itself, but we'll talk about that another day.) We just want to be sure that our code does what it's expected to do for each input possible, and still be correct. 

​It's possible to write tests strictly by yourself (for example, writing test files that call your project's code files, and just automatically call each function/class to test their correctness), and it may work on smaller projects, but as projects get larger and tests need to be more automated (we want to do as little as possible), it becomes better to use testing frameworks that make things a lot easier for us. (It helps by using multiple threads where possible, being efficient, giving us nice logging, etc.) 

Google C++ Testing Framework

Google has their own testing framework for C++ code, called Google Tests. You can click the previous link to see all of their principles and design choices, but it's a bit unnecessary. We're going to focus on using the testing framework in this article. Specifically, we're going to set up the Google Test library on Ubuntu.
This process is heavily inspired from the post on eriksmistad.no, so shout out to that author for being the one to teach me how to do this. (A lot of other tutorials sucked. Which is weird since this is a google library lol...You'd think google would be better at it.)

Installing Google Tests To Our Ubuntu Libs Folder

The first thing we want to do is install the Google Tests library on our Ubuntu machine, in the libraries folder where it may be called for any project we use Google Tests in. 

Use the following commands in your terminal to install Google Tests, install cmake, make/compile the Google Test project in our shared library folder. .
sudo apt-get install libgtest-dev
sudo apt-get install cmake
cd /usr/src/gtest
sudo cmake CMakeLists.txt
sudo make
sudo cp *.a /usr/lib

Using Google Tests On Some Code

Now we're going to run a test. We're going to write a function to check the square root of a number, and we're going to write a unit test to make sure that the function works correctly. 
Use the following commands to create the files that we'll need. (Make sure to open your terminal in your project folder. If you use the one from before, you'll be somewhere else.)
touch sqrt.cpp
touch sqrt_test.cpp
touch CMakeLists.txt
Open "sqrt.cpp" and add the following code (which has a function to calculate the square root of a double).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#include <math.h>
 
// Get the Square root of a number. 
double squareRoot(const double a) 
    {
    double b = sqrt(a);
    if(b != b) // NaN check
        { return -1.0; }
    else
        { return sqrt(a); }
    }
Now let's write a test file with some code that will use Google Tests to test the function. Open "sqrt_test.cpp", and write the following code. 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include "sqrt.cpp"
#include <gtest/gtest.h>
 
TEST(SquareRootTest, PositiveNos) 
    { 
    ASSERT_EQ(6, squareRoot(36.0));
    ASSERT_EQ(18.0, squareRoot(324.0));
    ASSERT_EQ(25.4, squareRoot(645.16));
    ASSERT_EQ(0, squareRoot(0.0));
    }
 
TEST(SquareRootTest, NegativeNos) 
    {
    ASSERT_EQ(-1.0, squareRoot(-15.0));
    ASSERT_EQ(-1.0, squareRoot(-0.2));
    }
 
int main(int argc, char **argv) 
    {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
    }
So our code and the unit test are written. Now we need to actually run the test file to see if the testing works. Open "CMakeLists.txt" and add the following to it. 
1
2
3
4
5
6
7
8
9
cmake_minimum_required(VERSION 2.6)
 
# Locate GTest
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})
 
# Link runTests with what we want to test and the GTest and pthread library
add_executable(executeTests sqrt_test.cpp)
target_link_libraries(executeTests ${GTEST_LIBRARIES} pthread)
The above code will compile our code into a file named "executeTests". We'll run this executeTests to run our tests. Run the following commands in your terminal.
cmake CMakeLists.txt
make
./executeTests
And you should see some pretty test output. 

Conclusion

The Google C++ Testing Framework is a nice framework to run our unit tests with. As you can see, our actual unit tests are very simple, and running the tests are really easy once you set everything up.

Now all of your code should be modularized and unit tests should be run for them! (And once again, shout out to the article that showed me how to do this.)
The video where I go over the installation and testing is here:
​Like this content and want more? Feel free to look around and find another blog post that interests you. You can also contact me through one of the various social media channels. 

Twitter: @srcmake
Discord: srcmake#3644
Youtube: srcmake
Twitch: www.twitch.tv/srcmake
​Github: srcmake
References
1. github.com/google/googletest/blob/master/googletest/docs/Primer.md
​
2. www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

Comments are closed.

    Author

    Hi, I'm srcmake. I play video games and develop software. 

    Pro-tip: Click the "DIRECTORY" button in the menu to find a list of blog posts.
    Metamask tip button
    License: All code and instructions are provided under the MIT License.

    Discord

    Chat with me.


    Youtube

    Watch my videos.


    Twitter

    Get the latest news.


    Twitch

    See the me code live.


    Github

    My latest projects.

Powered by Create your own unique website with customizable templates.