Quick Start with Google Test

Google Test is a framework from Google for unit testing of C ++ code. In general architecture, it slightly resembles the generally accepted boost :: test and CppUnit, although it is slightly different in details (as for me - for the better). A large review article of this framework already somehow ran on Habré, but now it is in some kind of broken condition (the code is not displayed), and it seems to me too complicated to get started. Therefore, I will briefly describe “Hello world” on Google Test, pointing out several potential problems that you may encounter when using Google Test when developing for Visual Studio.
Assembly
- Download the archive with the code , unzip it.
- There are two files in the gtest-1.6.0 \ msvc folder: gtest.sln and gtest-md.sln. These are Visual Studio Solution files. They differ in build options: gtest.sln collects code with the / MT switch, and gtest-md.sln with the / MD switch. If you do not know what these keys are responsible for, you can read, for example, here or here . You must compile the same version with which the project you are going to test is going. This is important, otherwise get a bunch of obscure linker errors. You can check what keys your project is going to here:
Google Test code is successfully compiled by Visual Studio 2008 \ 2010 (I haven’t tried it with others). At the output, you will get the gtestd.lib \ gtest.lib files (for debug and release configurations). With the assembly on it all.
Hello world
- We open the Solution which you are going to test. Add a new project to it (console C ++ application).
- In this project, we add the dependency on the gtestd.lib \ gtest.lib libraries compiled in the second step, the path to the Google Test include-folder, the dependencies on those projects in your solution that you are going to test.
- We write the following code in the main file of the test project:
#include "stdafx.h" #include "gtest/gtest.h" class CRectTest : public ::testing::Test { }; TEST_F(CRectTest, CheckPerimeter) { CSomeRect rect; rect.x = 5; rect.y = 6; ASSERT_TRUE(rect.GetPerimeter() == 22); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
Here we test a certain class of a rectangle for the correct calculation of the perimeter. You have noticed how convenient it is - you do not need to register each test in the main function, or even write declarations of test methods in the header files. - We launch a test project. We see the following:
Rake
Number one
Make no mistake with the choice of the compiled solution in the second step. If you make a mistake and forget, finding out what the mistake will be later will not be realistic.
Number two
If you plan to distribute the main test application and the tests themselves for different projects, you will encounter one tricky problem. The fact is that Google’s unit tests are essentially static classes, and the Visual C ++ compiler simply throws these classes out during compilation because of a bug. To avoid this bug you need to get out the way described here .
Number three
Do not forget that the tested static libraries do not need to be added to the Dependencies of the test project, they need to be added to the References, otherwise we will get link errors.
Additional materials
A little more in-depth articles on the Habre
Quickstart documentation in their native
FAQ
Advanced use framework
plugin for Visual Studio to run the test
luck in testing.
Happy New Year!