One of the Google C ++ unit test framework (gtest) series tutorials-getting started

Source: Internet
Author: User
Tags glob

Introduction

This article will first introduce the concept of unit test, then introduce Google's open-source C ++ unit test framework gtest, and finally compile and run a test sample that comes with gtest, this section describes how to use gtest in Unix/Linux.

Unit Test

When talking about unit testing, you should be familiar with it. As the lowest test stage in the software development process, unit testing is generally completed by the coding personnel, and its purpose is to isolateProgramParts, and prove that these individual parts meet the expected functions. In static program analysis,CodeUnit Testing after inspection can help us identify problems early in the development process. A good test has the following features:

1. Independent. A test case should be independent. The so-called "independence" means that the test results of this test case are not affected by other tests. The following example briefly describes the independence of unit test:

 1 # Include <assert. h>
2 Int Glob [ 3 ] = { 1 ,2 , 3 };
3 Int * G_p = glob;
4 Int Alloc ( Void )
5 {
6 Return * G_p ++;
7 }
8 Int Release (Void )
9 {
10 Return * G_p --;
11 }
12 Void Test_1 ( Void )
13 {
14 Assert ( 1 = Alloc ());
15 // Release ();
16 }
17 Void Test_2 ( Void )
18 {
19 Assert ( 1 = Alloc ());
20 // Release ();
21 }
22 Int Main ()
23 {
24 Test_1 ();
25 Test_2 ();
26 Return 0 ;
27 }

In the above example, the test_2 assertions will be thrown out. Obviously, its operation is affected by test_1. test_1 applies for (alloc) and should release (release ), to ensure that the state of the program is consistent before and after the use case is run.

2. Effective organizational structure and clear naming. Each test case is intended for different test objects. For a single test object, multiple test cases may have multiple functions for the object. A good habit is to organize these use cases in a hierarchical structure and use clear names so that we can understand the functions of the use case by reading the use case name.

3. Portable and reusable. Like our demanding requirements for platform-independent programs, we also hope to easily transplant excellent tests between different operating systems and compilers.

4. When the use case fails, provide as much valid information as possible. Undoubtedly, the clearer and more comprehensive the prompt information, the more convenient we can locate the problem and find out the bugs in the program efficiently.

Today, many testing frameworks help us complete unit tests, such as Google test and cppunit for C ++, cunit for C, and JUnit for Java, these testing frameworks enable us to implement independent, portable, reusable, and organized tests, allowing us to focus on Writing Test Code related to program functions. The following describes how to use gtest.

Google C ++ unit test framework

Google C ++ unit test framework (gtest) can be used on multiple platforms (including Linux, Mac OS X, windows, cygwin, and Symbian ), it provides a wide range of assertions, fatal and non-fatal failure judgments, value parameterization testing, type parameterization testing, and "Death testing ". Gtest is an open-source project. Its source code can be downloaded from here. The current code release version is 1.6.0.

Compile

The readme file in the source code package shows how to compile the gtest source code. The msvc and xcode directories contain project files related to Windows and Mac OS X, respectively, the cmake directory uses cmake for makefiles and project-generated files. If you are running Unix/Linux, you can directly execute the make command in the source directory to compile the files.

If the following error message is displayed during make execution:

 
./Src/gtest-death-test.cc:970:73: Error: 'clone 'was not declaredIn ThisScope
Make: * [src/gtest-all.lo] Error1

In the/src/gtest-death-test.cc file, place the 970th lines of code:

 
970Child_pid = clone (& execdeathtestchildmain, stack_top, sigchld, & ARGs );

Changed:

 
970Child_pid = fork ();

After compiling with make, you do not need to execute the make install command to install the header files and library files related to gtest. When compiling your own test code, you can reference the header files and library files from the current directory.

Execution case

The samples directory of the source code file provides multiple use cases. From sample1 to sample9, the author of gtest explained the usage of gtest from a simple perspective. How can we execute these use cases?

First, go to the make directory under the source code Directory, which contains a MAKEFILE file and execute the make command. After the make command is executed, several files are added to the directory, samplew.unittest is the executable file of sample1. Run./samplew.unittest to view the test result:

To generate executable files for other use cases for compilation, you can refer to the MAKEFILE file in the make directory or perform the following steps:

Copy the gtest-main.a dynamic library file to the samples directory and execute the following command:

 
$ G ++-I ../include/-C sample2_unittest.cc

And:

 
$ G ++-I ../include/sample2.o sample2_unittest.o gtest_main.a-lpthread-O Test2

Finally, we get the executable file Test2 of sample2. After these steps, we believe that everyone knows how to generate their own gtest execution file.

Summary

This article introduces some features of good test code, and briefly introduces gtest, describes how to compile gtest test code and generate gtest test executable files. NextArticleThis section describes how to use gtest, including assertion, function testing, firmware testing, parameterization, and death testing.

Reference: googletest Project

Google open-source C ++ unit testing framework-Google test series by coderzh

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.