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).
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.
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.
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.
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.
Comments are closed.
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.
License: All code and instructions are provided under the MIT License.