📜 ⬆️ ⬇️

Quick Start with Google Test


Google Test is Google 's framework for unit testing C ++ code. The overall architecture slightly resembles the generally accepted boost :: test and CppUnit, although it is slightly different in details (for me, to the better). A large review article of this framework has somehow run through Habré, but now it is in some broken state (the code is not displayed), and it seems to me too difficult to get started. Therefore, I will briefly describe “Hello world” on the Google Test, pointing out several potential problems that you may encounter when using the Google Test when developing under Visual Studio.


Assembly


  1. Download the archive with the code , unzip.
  2. There are two files in the gtest-1.6.0 \ msvc folder: gtest.sln and gtest-md.sln. These are the Solution Files in Visual Studio. They differ in assembly options: gtest.sln collects code with the / MT key, and gtest-md.sln with the / MD key. 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 that the project you are going to test is going with. This is important, otherwise you will get a bunch of unintelligible linker errors. Check with which keys your project is going to be here:


    ')
    Google Test code is successfully built by Visual Studio 2008 \ 2010 (I haven't tried it with others). At the output you will receive the files gtestd.lib \ gtest.lib (for debugging and release configurations). With the assembly on this all.


Hello world


  1. Open the Solution that you are going to test. Add a new project to it (console C ++ application).
  2. In this project, we add a dependency on the gtestd.lib \ gtest.lib libraries compiled in the second step, the path to the Google Test include folder, dependencies on the projects in your solution that you are going to test.





  3. We write the following code in the main test project file:
    #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 are testing a certain class of rectangle for the correctness of the perimeter calculation. You have noticed how handy - you do not need to register each test in the main-function, or even write test method declarations in the header files.

  4. We start the test project. We see the following:



Rake


Number one

Make no mistake with the choice of compiled solution in the second step. If you make a mistake and forget - to find out what the error will later be actually not real.

Number two

If you plan to spread the main test application and the tests themselves for different projects, you will encounter one tricky problem. The fact is that Google units are essentially static classes and the Visual C ++ compiler, because of the bug it has, will simply throw out these classes during the compilation process. To avoid this bug you need to get out the way described here .

Number three

Do not forget that the tested static libraries need not be enough to add in the dependencies (Dependencies) of the test project, they need to be added to the references (References), otherwise we will get linking errors.

Additional materials


Slightly deeper article on Habré
Quick start in native documentation
Frequently asked Questions
Advanced use of the framework
Visual Studio Plugin to Run Tests

Successes in testing.
Happy New Year!

Source: https://habr.com/ru/post/164471/


All Articles