Setting up Eclipse CDT and Google Test for C development

This post Builds on my previous post, https://bjornarnelid.wordpress.com/2014/03/10/how-to-get-started-with-google-test-in-eclipse-cdt-on-linux/ . Follow that guide to install Google Test and Eclipse CDT.

I really like Google Test, its great as a test framework, and i really like working in it when doing TDD style developing in C++. When it comes to C the choice of testing framework is not as easy. There are things like CUnit and check, but for me they don’t give as good Eclipse integration and flexibility as Google Test does. So wouldn’t it be great if you could test your C code using Google Test?

It turns out that you can, and its not as difficult as you might think, here is how!

Install Eclipse CDT and Google Test as usual

If you dont know how to do thism, dont worry! Just follow my previous guide here!

Create your Test Project

This will be quite similar to how you create your C++ Project in my previous post.

  1. Create a new C++ project in Eclipse using File -> New -> C++ Project
  2. Since this will only be your test project you only need to create one source folder.
  3. Go into properties for the test folder by right clicking on the folder and selecting properties
  4. Select C/C++ Build ->Settings
  5. Under GCC C++ Compiler -> Includes. Add <google test folder>/include
  6. Under GCC C++ Linker -> Libraries. Add the google test build folder and the libraries gtest and pthread
  7. If Eclipse still shows errors, you can try to rebuild your index.

Create your Application Project

Create a C project as usual where you will develop your application. Make a separate c file containing an empty main method, alternative move your main method to a separate file.

Doing this will let you compile your application project as usual and import application logic into your test project.

Setup your Test Project To Work With Your Application Project

  1. Go to YourProject -> Properties -> C/C++ Build -> Settings -> Includes
  2. Add you C application source folder as a include path
  3. Go to GCC C++ Linker Miscellaneous
  4. Add your C project .o files as Other objects

You are now ready for development! Its not as straight forward as the C++ solution, but its working well enough.

Tests are not (just) for testing!

“I don’t have to test that, because i know it working.”

At first glance this quote seems to make sense. The algorithm is working, any fool could see that, so it would be a waste of time and space to write a test proving the obvious. And its an explanation i have heard from senior and junior developers alike.

For me, that is totally beside the point. Of course he knows that it works! He wrote the darn thing, and i must at least assume that he tried to compile and run it before committing. Though even if the code is simple it will only tell me what it does, not why.

I watch a unit test as a description of intent, more then anything else. A unit test can tell me what problem a function is designed to solve when the code itself tells me how it is solved. Just as for the rats in Hitchhikers guide to the galaxy sometimes just knowing the answer (42 by the way) isn’t enough, sometimes we need to understand the question as well.

How to get started with Google Test in Eclipse CDT on Linux.

Setting up your development tools is an important first step in TDD, and its not always as easy as one could think.

I found that setting up my C++ development environment a little bit troublesome, and there wasn’t that many good how-to:s out there..

Here is How i get up and running with Eclipse CDT and google test.

Install Eclipse with Unit test support

  1. Install Eclipse CDT from https://www.eclipse.org/cdt/
  2. In Eclipse go to  Help -> Install New Software…
  3. Select default Eclipse Update site in the Work with field, for me its  Kepler – http://download.eclipse.org/releases/kepler
  4. Install package C/C++ Unit Testing Support under Programming Languages, you will need to restart your IDE once the installation is done.

Get and compile Google Test

  1. Download google test from https://code.google.com/p/googletest/
  2. Unpack google test somewhere on your computer.
  3. Create a google test build dir.
  4. cd into your build directory and type “cmake <google test directory>”  and “make” to build google test.

Configure a C++ Project to use Google Test in the same project

  1. Create a new C++ project in Eclipse using File -> New -> C++ Project
  2. Create a source folder to hold your tests by right clicking on your project and choosing New -> Source Folder
  3. It is usually a good idea to not include your tests in your release build so right click on your test folder and select ->Resource Configurations -> Exclude from Build… and exclude the folder from Release.
  4. Go into properties for the test folder by right clicking on the folder and selecting properties
  5. Select C/C++ Build ->Settings
  6. Under GCC C++ Compiler -> Includes. Add <google test folder>/include and the folder where your project source files will be.
  7. Under GCC C++ Linker -> Libraries. Add the google test build folder and the libraries gtest and pthread
  8. If Eclipse still shows errors, you can try to rebuild your index.

Create a launch configuration

  1. Once you have written your first test, build your project, and go into the Run ->Run configurations… menu.
  2. Double click on C/C++ Unit to create a unit test configuration.
  3. In main tab select your project and the binary you just built.
  4. Go to C/C++ Testing tab and select the google tests runner
  5. Click run and your tests should now run

Once everything is set up using eclipse and google test will be quite easy and convenient to work with!

Hello World!

Apparently i now own a blog, and i must start creating material for it immediately! Because if there is one thing the Internet is lacking, that thing must surely be user made never read content.

I might even create something useful here, like a tutorial or something, but do not count on it!