Last Updated: February 25, 2016
·
12.95K
· pablosproject

Configure Eclipse for TDD with C++ and Google C++ Testing

The first thing to do is create a new project for testing. I have a project called MyProject, and then I create a new project called MyProjectTesting.
I have all the source code in a folder called src.

Download the Google C++ Testing Framework at these link : <a>http://code.google.com/p/googletest/downloads/detail?name=gtest-1.6.0.zip&can=2&q= </a>.
Make a folder into your project and name it testinglib, and then extract the Google Testing framework with the following command (from the Google Test folder):.
*/scripts/fuse
gtestfiles.py . <project-dir>/testinglib*

Now the most important part, create a folder and name it imported_src. While you're creating it select advanced, and then Link to alternate location, and select the src folder in MyProject, like in these screenshot.

Picture.

Now you've a mirror to your src folder. Select the file containing the main, and right-click. Select Exclude from build, and select all the build profiles.

Now we need to tell the compiler how to link the test library with the source file. First of all create a folder in your project and name it test; then right-click on test ->properties->C/C++ Build, and then add to the Includes->Incude Path two folders: the first is the folder with the test library, that we call testing_lib, and the other id the original source file folder, called imported_src, as showed in these screenshot.

Picture

Make the same thing with the testing folder, but these time link the folder with itself. These is an important step, if you don't do this, the compiler'll complain.

Now in the test folder add a file called MainTest.cpp and paste the following code:


include "gtest/gtest.h"

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUNALLTESTS();
}
</pre>

This will launch all the tests (these step is not necessary because the testing framework has an auto-main option). After that write a first test case, let's assume that I have a class called Sin that implements a simple sin function. Now I write a simple test case using GoogleTest rules, and launch it, the result is showed here:

Picture

You've done! Now you can write all the test case that you want and Google Test Framework'll execute all the tests on your source code without modifying it.

3 Responses
Add your response

Hi, thanks for this great post. But I wanna ask if there's a way to configure google test libs once in eclipse and then linked it to any wanted project instead of make the same process for every single project. Thanks in advance

over 1 year ago ·

This maybe help if you are still looking for answers. Once get the shared libraries, you can just put the -l and -L options on the build options.

http://stackoverflow.com/questions/13513905/how-to-properly-setup-googletest-on-linux/21314020#21314020

over 1 year ago ·

Hi Paolo,
Thanks for the info. However I don't understand some of the points.
For example where did you place the unzipped gtest files?
Do they have to be in the eclipse project?


Make the same thing with the testing folder, but these time link the folder with itself. These is an important step, if you don't do this, the compiler'll complain.


Could you explain what you mean by testing folder?
Please give an example.

I'm getting errors from the compiler and not understanding the above may be the cause.

Thanks in advance

over 1 year ago ·