Steps for setting up the GoogleTest (GTest) test environment in Linux

Source: Internet
Author: User
This article describes how to set up the unit test tool GoogleTest (GTest) in the Linux operating system test environment. This document is a basic tutorial for Google test. Before using GoogleTest in Linux, you need to have some knowledge about the following: (1) C/C ++ programming method (2) Compiling makefile (3) Linux Command Line Operations (4) GTEST Background: What does it do? What language does it use? What are its basic syntaxes? And so on.

This article describes how to build a unit Test tool Google Test (GTest) in a Linux operating system. This document is a basic tutorial for Google Test. Before using Google Test in Linux, you must have the following knowledge:

(1) C/C ++ Programming Method

(2) Write makefile

(3) Linux Command Line operations

(4) GTEST Background: What is it used? What language does it use? What are its basic syntaxes? And so on.

The above knowledge does not need to be practical. As long as it reaches the entry level, GTEST will not be used with any effort. After all, GTEST is not a high-threshold tool. Let's take a step by step:

Step 1: Download GTEST

Download link: https://code.google.com/p/googletest/downloads/list

Currently, gtest-1.7.0.zip is the latest version of gtest. therefore, this version is used as an example in this article.

Step 2: Recognize folders

After downloading and unzipping, you can see the gtest-1.7.0 folder, as shown in

At first glance, such a large number of files are a bit confusing. In fact, GTEST provides support for multiple different platforms. For example, msvc folders are used in Microsoft Visual Studio, xcode folders are used in Mac Xcode, and codegrear folders are used in Borland C ++ Builder, in Linux, the content we use is the make folder.

Step 3: Clear unnecessary files

As we mentioned in the previous step, many files are used to support different platforms. In order to keep the program concise and avoid confusion, we will delete all unnecessary files in this step, only what we need is retained. In fact, we only need four folders, as shown in. Delete all the other files and folders (this step is not required, but for our clean programmers, there is no need to put a bunch of unused code ~).

Okay, there are only four folders left. Does it seem a lot refreshing? Open the make folder and you will find that there is only one Makefile in it. View the contents of the Makefile file and find that this is the first sample command in the compilation samples folder provided by the system. But open the sample folder and you will see a large batch of source files. In this tutorial, we will not consider complex examples. Therefore, open the samples folder, Start deleting the file, and delete the remaining three files.

Our program is getting refreshed.

Step 4: rewrite the Makefile file

Now, if you go to the make folder and run the $ make &./samplew.unittest command through the command line, you can see the test execution result. However, if you open Makefile, you will find that this makefile is only applicable to the compilation of sample1. What if I add another source file to be tested? It is too troublesome to write makefile again. In this step, we will rewrite the Makefile.

As we mentioned in the previous step, there are only four folders (include, make, samples, src) left. Since the only file in make is to be rewritten, there is no need to leave this folder. So in Step 4, the first thing you need to do is to delete the make folder, along with the Makefile file ...... Then, go to the samples folder and create a file named Makefile,

Then, open the Makefile file and write the following content (do not write the row number in the graph as well ~). This new Makefile was rewritten by the Makefile we just deleted. If you are curious, you can compare the differences between them, which involves some makefile syntax and functions, if you are not familiar with it, it may take several minutes to check the information to learn about them.

Note that the Makefile 32nd line is rewritten in. We compile a file with the suffix cpp, but the original example ended with cc. Therefore, you need to change the sample1.cc file name to sample1.cpp and change the samplew.unittest.cc file name to sample=unittest.cpp.

Now we have three folders (include, src, samples), and the program we tested is placed in the sample folder. The folder name is also quite uncomfortable, you can change it to mycode or testcode, and then GTEST root directory folder name gtest-1.7.0 can also be changed to mygtest and so on, to meet our perfectionist needs. :

Now, go to the command line to compile and execute: $ make &./run_test. The result is displayed ,:

Step 5: add your own test function

In the above step, our test environment has been set up. If you want to test a function, continue to Step 5.

Suppose we have a function named sqrt. cpp and its header file sqrt. h. Their contents are as follows:

(Sqrt. cpp)

(Sqrt. h)

(Sqrt_unittest.cpp)

This file is used to calculate the square root of any positive integer. The algorithm complexity is at the log (n) level. Put the above three files in the mycode folder, and then make &./run_test for compilation and running. The result is displayed:

Conclusion: After the environment is set up, every time you test a file xxx. cpp and its xxx. h file, put the two files in the mycode folder, write the xxx_unittest.cpp test file, and put it in. Then you can use the command line make &./run_test in this directory, which is much easier than the most convenient time? To test, we only need to put three files into mycode, and then run the command line to enter the directory make &./run_test to complete the test. The following figure shows my current mycode folder. The file name xxx_unittest.cpp is not fixed, and it doesn't matter if you get it into something else, but it is easier to identify which file is testing which function.

Of course, Google Test is a very powerful tool. All the above is about using its most basic functions and setting up a simple environment. In fact, the file to be used is the content of the remaining three folders mentioned in Step 4, which can be modified to implement more powerful functions. Wait until you have the opportunity to write it again.

For more information about Ubuntu, see Ubuntu special page http://www.linuxidc.com/topicnews.aspx? Tid = 2

This article permanently updates the link address: Http://www.linuxidc.com/Linux/2015-05/116894.htm

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.