Testing C++ code using GoogleTest on Eclipse (MinGW ToolChain)

I’ve been trying to test a very simple piece of code using googleTest, and it took me 3 days to get it done. The main issue was for me to be able to set the environment. This post is on how to set the environment, followed by the list of mistakes I’d done.
Basically what I wanted to do was to keep my project separate from my tests, so that the tests would go into a separate project.

  1. Go to GoogleTest on GitHub where you can find documentation, code, and a couple of examples on how to use it. You can clone it into your local or download it.
  2. Open Eclipse. Let’s say we are testing a project called base_project, now you need to create another project called unit_test maybe.
  3. In your test project you need below to be created.One folder to hold googletest libraries, let’s call it googleTestLib, and a folder to hold your unit tests, let’s call it tests
  4. Using your command prompt, cd into where you have googleTest stored. In this path \googletest\googletest\scripts, there should be a file called fuse_gtest_files.py. Run the script, as usual, using .\ fuse_gtest_files.py {the project location}\ googleTestLib . Refreshing your unit_test project, googleTestLib should now have a folder called gtest with a gtest.h and gtest-all.cc file inside it.5- Right click on googleTestLib folder, go to resource configuration, and exclude it from “release”
  5. Right click on googleTestLib folder, go to properties, under C/C++ Build click on settings, find the include link under GCC C++ Compiler and click on it. Now add the folder (googleTestLib) as an include path
    here by clicking on Add button.
  6. Let’s say in your base_project, you have a class called base_class and you want to write a test for it. The base_class is probably in a ./src folder which you need to link to unit_test project so that you can include it in your headers later. To do this, right click on your unit_test project, go to properties, find paths and symbols under C/ C++ General, click on Source Location, find the Link Folder button, tick the option saying Link to Folder in the file system, browse and find the folder(s) including all the .cpp and .hpp files you need for your tests to run.
  7. As soon as the above step is done, you’ll see folders showing links to your base_project, being displayed in your unit_test project. Make sure you exclude these from “build” and “release”. The folders will have the same name they had in base_project.
  8. So recall that you’d created a test folder to put your tests in? Now you can create your test source files in this folder, however, you have to “include” all the necessary folders in this folder. To do this right click on test folder, go to properties, under C/C++ Build click on settings, find the include link under GCC C++ Compiler and click on it. Include googleTestLib and the folders you linked from your base_project. Make sure you include these from your unit_test project and not from base_project.

So the mistakes I’d made were:

  1. I had a main function in my base_project which would collapse with the main in my test source file.
  2. I didn’t exclude the folders I’d linked to unit_test from base_project, so I was getting re-definition errors.
  3. I excluded the test source file with main function from build so I was getting a winmain@16 error. I don’t know why I did it, and then I spend 2 hours trying to figure out why eclipse thinks I need a winmain function instead on main
  4. Instead of including the folders I’d linked, I included the folders from the base project.
  5. One of my files had a .c extension instead of .cpp (I know! Don’t roll your eyes)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s