I. Preface
This article describes some basic usage of gtest, including downloading, installing, compiling, creating our first test Demo project, and compiling a simplest test case.
Ii. Download
If you do not remember the URL, search for gtest in Google. The first one is. The latest version of gtest is 1.3.0. You can download the latest version from the following address:
Http://googletest.googlecode.com/files/gtest-1.3.0.zip
Http://googletest.googlecode.com/files/gtest-1.3.0.tar.gz
Http://googletest.googlecode.com/files/gtest-1.3.0.tar.bz2
Iii. Compilation
After downloading and unzipping, there is a msvc directory:
If you are using vs2005 or vs2008, you will be prompted to upgrade the version, we can compile the "gtest" project directly.
Please note that, if you upgrade your project to vs2008, your test demo is also the vs2008 project. Otherwise, you will find it very depressing and your demo cannot be compiled, I also struggled for a long time. At that time, I upgraded to the vs2008 project. As a result, I used the vs2005 project to create a demo, but I could not make it. (This is a misunderstanding. It does not mean that it can only be compiled in vs2008, but also in vs2005. If you want to compile the vs2005 version, it is best to ensure that both gtest and your test project use the vs2005 project .)
After compilation, the compiled gtestd. lib or gtest. Lib file is displayed in the debug or release directory of msvc.
4. first demo
Next we will start to build our first demo. If we used the gtest compiled by vs2008, we will create a Win32 console application in vs2008. Set the project properties as follows:
1. Set the path of the gtest header file
2. Set the gtest. Lib path
3. Runtime Library settings
For the release version, set the Runtime Library to/mt. Of course, you can also choose dynamic link (/MD), provided that the gtest you compiled earlier also uses the/MD option.
After the project settings, let's write a simple test case. Let's first write a tested function:
Int Foo ( Int A, Int B)
{
If ( = 0 | B = 0 )
{
Throw " Don't do that " ;
}
Int C = A % B;
If (C = 0 )
Return B;
Return Foo (B, c );
}
Yes, the above function is used to calculate the maximum public approx. Next we will compile a simple test case.
# Include < Gtest / Gtest. h >
Test (footest, handlenonezeroinput)
{
Expect_eq (2, Foo (4,10));
Expect_eq (6, Foo (30,18));
}
The above shows how simple it is to write a test case. We use the macro test, which has two parameters. The official interpretation of these two parameters is [testcasename, testname], and my definition of these two parameters is: [testsuitename, testcasename]. In the next article, Let's see why this definition is made.
The expect_eq macro is used to check the checkpoint. This macro is used to compare whether two numbers are equal. Google also encapsulates a series of objective CT _ * and assert _ * macros. The difference between the objective CT Series and the assert series is:
1. When CT _ * fails, the case continues to be executed.
2. When assert _ * fails, it is returned directly in the current function. The statements following assert _ * in the current function will not be executed.
In the next article, we will discuss these asserted Macros in detail. In order for our case to run, we also need to add the following in the main function:Code:
Int _ Tmain ( Int Argc, _ tchar * Argv [])
{
Testing: initgoogletest ( & Argc, argv );
Return Run_all_tests ();
}
"Testing: initgoogletest (& argc, argv);": The gtest test case allows receiving a series of command line parameters. Therefore, we pass the command line parameters to gtest, perform some initialization operations. The command line parameters of gtest are rich and will be detailed later.
"Run_all_tests ()": Run all test cases
OK, everything is ready. Let's run the case directly (a piece of green, very nice ):
V. Summary
The content of this article is indeed very elementary, with the aim of letting people who have never been familiar with gtest understand the most basic usage of gtest. Gtest has many more advanced usage methods, which we will discuss later. To sum up the content of this article:
1. Use vs to compile the gtest. Lib File
2. Set the properties of the test project (header file, lib file,/MT parameter (just use the same parameter as when compiling gtest ))
3. Use the test macro to start a test case. Use the train CT _ * And Asser _ * series to set checkpoints.
4. initialize the environment in the main function and run the test case using the run_all_test () Macro.
Advantages:
1. Our test case itself is an EXE project, which can be run directly after compilation, which is very convenient.
2. Writing Test cases is quite simple (using some simple macros such as test), so that we can spend more time designing and Writing Cases.
3. provides powerful macro with rich assertions for checking various checkpoints.
4. Improved the extensive set of command line parameters for case running.
Series links:
1. Go to Google's open-source C ++ unit testing framework, one of the Google test series (gtest)-First known as gtest
2. Go to Google open-source C ++ unit test framework Google test series (gtest) 2-Assertions
3. Go to Google open-source C ++ unit test framework Google test series (gtest) 3-event mechanism
4. Go to Google's open-source C ++ unit test framework: four parameters of the Google test series (gtest)
5. go to Google open-source C ++ unit testing framework Google test series (gtest) 5-death Testing
6. go to Google open-source C ++ unit testing framework-Google test series (gtest)-running parameters
7. Go to Google open-source C ++ unit test framework Google test series (gtest) 7-in-depth analysis of gtest
8. go to Google open-source C ++ unit testing framework Google test series (gtest)-build your own unit testing framework