Testing CPP using GoogleTest
Nina Rallies
Posted on September 15, 2018
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.
- 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.
- Open Eclipse. Let's say we are testing a project called base_project, now you need to create another project called unit_test maybe.
- In your test project you need below to be created:
- A folder to hold googletest libraries, let’s call it googleTestLib
- A folder to hold your unit tests, let’s call it tests
- 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.
- Right click on googleTestLib folder, go to resource configuration, and exclude it from “release”
- 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.
- 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.
- 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.
- 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:
- 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.
- I didn’t exclude the folders I’d linked to unit_test from base_project, so I was getting re-definition errors.
- 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
- Instead of including the folders I’d linked, I included the folders from the base project. :-D
- One of my files had a .c extension instead of .cpp (I know! :-D)
💖 💪 🙅 🚩
Nina Rallies
Posted on September 15, 2018
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.