From the course: Test-Driven Development in C++

Set up Google Test with Eclipse - C++ Tutorial

From the course: Test-Driven Development in C++

Set up Google Test with Eclipse

- [Instructor] In this lecture, I'm going to walk through downloading and compiling Google Test C++ unit testing framework. And then getting it set up to run in the Eclipse CDT C++ IDE. The framework is patterned on the typical X unit type testing frameworks and compiles on Linux, Windows, and Mac environments. To follow along with this lecture, you'll need to have a recent version of the Eclipse CDT IDE installed. CDT stands for CC++ development tool. This is a C++ flavor of the popular open source Eclipse IDE. You'll also need to have C++ compiler and CMake installed. The most common C++ compiler from any Posix environment is the Gnu C++ compiler. Another option is the CLion C++ compiler. In addition to that, you'll need to have CMake installed for the build process. For the lecture, I'm gonna be working in Mac OS 10 but the set of steps to build Google Test and integrate it with Eclipse CDT should be essentially the same in Linux or Cygwin on Windows. The Google Test library's stored on GitHub. So the first step is I'm going to get the latest source code for the Google Test library by cloning the Git Repo from the command line. So, let's go ahead and do that now. First, I'm going to copy the URL for the repo. And then for my command line, git clone and that URL. And that downloads all the latest source code for Google Test into a Google Test directory. Okay. Now I'm gonna clone the repo. I can go ahead and build the library. Google Test uses CMake for building the library in Posix type environments, like Linux, Mac OS, and Cygwin on Windows. So, first I'm going to make a build directory and then I'm gonna run CMake from that directory. CMake will then generate the make file I need to build the library. Make directory build. Cd build. CMake dot dot slash. And now I see my make file. Once CMake is completed, I can list the contents of the build directory and see the make file. Now I can run make and actually build the library. Now that the build's complete, I can see there's a Google Mock folder in the build directory, and inside that is the Google Test directory. The Google Test directory contains the output library files that we'll be using. That's libgtest.a and libgtest_main.a. Google Mock is the library for creating mock objects, which are test objects that can be very useful during unit testing. Google Mock was combined with the Google Test library recently, which is why you see references to it here. I'll be providing a course on advanced tester developments C++, where I'll go over the usage of Google Mock to help you with getting your unit tests to run in isolation. Now that I've got Google Test built, I can look at integrating it with the Eclipse CDT IDE. To do that, I need to install the Eclipse C++ unit testing support plugin. I'll do that by bringing up the Install New Software window, selecting the Eclipse download site from my version of Eclipse, and then selecting the C++ unit testing support plugin in the programming languages section. So, let's go ahead and do that now. I'll look at Help, Install New Software. And I'll install from the Eclipse download site. Okay. So, now I'll go to the programming languages section. And I will select CC++ Unit Testing Support. Click Next. And select that. Click Next. I accept the license agreement. And then Finish. And now Eclipse will need to restart. Okay. Now that I have the plugin installed, I'll set up a new C++ project to test that I can create the C++ binary, using the Google Test libraries. I'll also set up a run configuration using the Google Test test runner that is installed with the C++ unit testing support plugin. First, I'll create a new C++ project and I'll verify that it compiles and shows the hello world output. So, let's do File, New, C++ Project. GT Tests. Mac OS 10. Hello World. Okay, let's see if that compiles. It does. And let's see it execute. Can it run successfully? Okay. Now that I have the project set up, I'm gonna go ahead and modify the code for simple Google Test assert, unit test that should always pass. So, first I include the Google Test library. Header file. And I'm gonna delete name. So, I should be included with the Google Test libraries. Okay. Okay. When I try to compile this, this doesn't compile right because the project doesn't know about the path to the Google Test headers. Let's go ahead and fix that now. So, I'm gonna right-click on the project and then click on Properties. Then I'm going to go to C++ General Paths and Symbols. Under Gnu C++ I'm gonna add a new include directory path. Okay. Let's see how that builds. Okay. That gets me past the problem of the include file, but now the linker's complaining. It looks like it's complaining that it doesn't have a main function. The main function is actually included with Google Test. So I need to update the project to know the path to the Google Test libraries and to link with those libraries. So let's do that now. Right-click on the project, select the properties. Under Library Paths, let's add the path to the Google Test libraries. So we'll need Google Test. And Google Mock is where the actual libraries are. Under Build, Google Mock. And then gtest. Under gtest we have libgtest_main and libgtest. So, we'll add the path to that directory and then we will add gtest_main and gtest to our list of additional... Libraries. And now compile successfully. And if I execute it, it executes successfully. But it's a simple text output. I want to have the green bar like I had with the fizzbuzz example. I'm going to create a new run configuration using the Google Test test runner installed with the C++ unit testing plugin. So, to do that, I'm gonna click on the down arrow next to the Run button. I'm gonna select Run Configurations. From there I'm going to add a new C++ unit test. So, I'm gonna call it GTTests Unit Tests. And then I'm going to select the Google Test Runner. Which I run now. Looks like everything passes successfully and we get our green bar. At that point, we're all set. I've got a C++ project in Eclipse using Google Test and a test runner showing me the results of my test. In the next lecture, I'll walk through the same process but in Windows with Visual Studio.

Contents