Getting started with gtest in Eclipse Juno under Mac OS

As part of my project of trying to get in to test driven development I read this blog post by Grzegorz Gałęzowski which gives a brief introduction on how to use Eclipse Juno and C/C++ Unit. My experience was that it wasn’t quite as simple as described to get things running under Mac OS. This post aims to provide detailed instructions on how to get gtest and C/C++ Unit working in Eclipse under Mac OS.

Install C/C++ Unit

Eclipse doesn’t come with the C/C++ Unit plugin installed by default, fortunately Eclipse makes it easy to browse for and install new software. To install navigate to “Help” > “Install New Software…”.

For the “Work with” field select “Juno – http://download.eclipse.org/releases/juno” and wait for the list of available plugins. Under “Programming Languages” select “C/C++ Unit Testing Support” and click next until you’re done. Now click “Window” > “Show View” > “Other…” and find “C/C++ Unit” under “C/C++” and press “OK”. You should now see the C/C++ Unit view in your workspace.

Install gtest

Download gtest from here and extract the contents to a folder of your choice, I’m using ~/tmp. This will get you the header files (they are located in the include folder), but we also need to compile the libraries. Fortunately this is straight forward:

cd ~/tmp/gtest-1.6.0
./configure && make

The compiled libraries can be found under ~/tmp/gtest-1.6.0/lib/.lib/. The files of interest are libgtest.a and libgtest_main.a.

Let’s move the files we need to a common place, say ~/dev/frameworks/:

mkdir -p ~/dev/frameworks/
cd ~/dev/frameworks/
mkdir -p gtest/{lib,include}
cd gtest
cp ~/tmp/gtest-1.6.0/lib/.libs/*.a lib/
cp -R ~/tmp/gtest-1.6.0/include/ include/

Moving on!

Setting up Eclipse

The first step is to create a C++ project. This is done by pressing “File” > “New” > “C++ Project”. Select “empty project”, give your project a name and select a toolchain. I’m using “Cross GCC”.

Now right click on your project and click “Properties” and navigate to “C/C++ Build” > “Settings”. Under “Cross G++ Compiler” > “Includes” add ~/dev/frameworks/gtest/include as an include path. Under “Cross G++ Linker” > “Libraries” add gtest_main and gtest as libraries and ~/dev/frameworks/gtest/lib as library search path. If you placed your files under /usr/local/lib/ (or some other path already in the default search path) in the previous step you don’t need to specify a path. Anyway, you should now have something that looks like this:

Create a build target for running tests

With that sorted we’ll soon be ready to write some tests, but we still miss two things: the build and run configurations.

To create a test build target right click on your project and navigate to “Build configurations” > “Manage…”. Click “New” and give it an appropriate name, I’m calling mine “UnitTest” and choosing to copy settings from “Debug”. You should now have three build targets, namely Debug, Release and UnitTest:

Build configuration
Build configuration

Before we continue with creating the run configuration we need to create some basic directory structure and files. Create one folder named “test” that will contain our test code and one named “src” for application code that we want to test. Inside the “test” folder create an empty C++ file, I chose to name mine “first_test.cpp”. In the “src” directory create a empty file called “main.cpp”, this file will be the entry point to our application, aka house the infamous main function.

When building our product code we don’t want to build all our test code so we’ll exclude our test folder from the “Debug” and “Release” target. This is done by right clicking the “test” folder and selecting “Resource Configurations” > “Exclude from Build…” and check “Debug” and “Release”. Also, when running our tests the main function in main.cpp will interfere with the main function in gtest_main (this is why we included it). To fix this simply exclude main.cpp from the “UnitTest” target. Now build the “UnitTest” target as this is needed for the next step.

We are now ready for the final step: creating our run configuration. I though this was supposted to be done automatically by Eclipse, but it doesn’t look that way, at least not in Mac OS. For me Eclipse  was complaining about not finding the binary to run.

Click “Run” > “Run Configurations…” and click on “C/C++ Unit” in the menu. Now create a new configuration named “UnitTest” and browse to the location of your built binary under the “UnitTest” build folder, for me the path was ~/dev/workspace/TempProject/UnitTest/TempProject. Select your project and under “C/C++ Testing” select “Google Tests Runner” and press “Run”. Nothing will happen really, but you should not be getting any errors.

You might need to create a run configuration for your application code as well, if it doesn’t run by default. The process is exactly the same as described above, except that you don’t select a test runner. Now let’s try to get some tests running!

See it in action

Below is the source code i put in first_test.cpp. The tests doesn’t really test anything in particular, but they will tell us if C/C++ Unit is working.

#include <gtest/gtest.h>

TEST(MyFirstTest, AssertToFalse)
{
	ASSERT_FALSE(false);
}

TEST(MyFirstTest, AssertToTrue)
{
	ASSERT_TRUE(true);
}

TEST(MyFirstTest, TwoGreaterThanOne)
{
	ASSERT_GT(2, 1);
}

If the flying spaghetti monster is good you should see this:

How C/C++ Unit should look if the tests pass
How C/C++ Unit should look if the tests pass

That is, now go write some tests!