In this article I will have a look on how to get started with Google Test libraries on Windows using Qt Creator for both MinGW and Visual C++.
I used the plural for Google Test libraries because there is Google Test – Google’s C++ test framework and also Google Mock – Google’s C++ mocking framework. They both are hosted on a single location on github.
Unfortunately the 2015 migration from Google Code to Github broke a lot of documentation search page links for Google Test, not to mention that the code snippets lost the syntax highlighting.
Here are the updated links for Google Test Primer and Google Mock for Dummies.
I will assume you have Qt Creator, CMake (and Ninja), MinGW and Visual C++ installed.
First step would be to get the master bundle zip package for both Google Mock and Google Test libraries.
Then unpack the
googletest-master.zip file into a directory e.g.
Then create a
CMakeLists.txt file with the following content:
Looks simple enough.
add_subdirectory (googletest-master) will add the GMock and GTest include directories so we don’t have to.
set (gtest_disable_pthreads on) is needed for MinGW, otherwise we will get errors like:
config_compiler_and_linker() is required for Visual C++, which otherwise we will have linking errors like:
Now all that is needed is the code for
I took the code for
mock_turfle_test.cpp from the Google Mock for Dummies tutorial.
The code mocks the
Turtle interface and makes sure that
Painter::DrawCircle will issue a call to
100 and 50 argument values, and a call to
CMakeLists.txt file with Qt Creator and compile and run the project! Here is a screen-shot from my machine:
But what happens if a tests fails? I have changed the argument from
DrawCircle from 100 to 101. If I compile and run
I will get the following:
We can see that the test has failed. But how can we go to the line that failed? Qt Creator has highlighted the error, but it can’t actually go to the line in question.
Test runner MinGW
Since Google Test will output the file and line that failed, we just need to make Qt Creator to parse the output.
We will achieve this by adding a simple line in
Now we have a new target to the project named
unittest which will run our test. But how do we run this
target from Qt Creator? By typing
cm (shorthand for cmake) in the locator bar!
After running the
cm unittest the following happened:
We can see that in the bottom right the build progress bar is red and we got a list of issues. After double-clicking the first line we jumped to the line that failed
Qt Creator should have treated this failure as an error and should have shown an error icon at line 50. I have opened up QTCREATORBUG-15505.
Test runner Visual C++
Compiling and running the failure test looks like this:
We can see that the error is being highlighted, which means that the output is parsed.
Now let’s try
The build is marked as red, but unfortunately the issues list is empty!
I have open up QTCREATORBUG-15506.
Hacking Google Test
I have noticed a difference between MinGW and Visual C++ GTest error lines:
|Visual C++||C:\Projects\gmock\turtle\mock_turtle_test.cpp(50): error:|
By applying the following patch:
I was able to get this for MinGW:
Respectively for Visual C++:
Using Google Test with Qt Creator is easy to setup and, with a bit of hacking, easy to use!