DEV Community


Testing CPP using GoogleTest

nina_rallies profile image Nina Rallies ・3 min read

I've been trying to test a very simple piece of code using googleTest, discussed here!, and it took me a couple of 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 GoogelTest - 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:
    1. A folder to hold googletest libraries, let’s call it googleTestLib
    2. 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 Run the script as usual using .\ {the project location}\ googleTestLib . Refreshing your unit_test project, googleTestLib should now have a folder called gtest with a gtest.h and file inside it.
  5. Right click on googleTestLib folder, go to resource configuration, and exclude it from “release”
  6. 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.
  7. 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.
  8. As soon as 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.
  9. 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 collide with the main in my test source file.I had to exclude or remove it altogether.
  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 spent 2 hours trying to figure out why eclipse thinks I need a winmain function instead of main :-D
  4. Instead of including the folders I’d linked, I included the folders from the base project. :-D
  5. One of my files had a .c extension instead of .cpp (I know! :-D)

Discussion (0)

Forem Open with the Forem app