This article describes the procedure of setting up unit testing tools Google Test (GTest) in the Linux operating system in the test environment. In this paper, a basic tutorial Google Test uses. Before using Google Test in Linux, you need to have some understanding of knowledge as follows:
(1) C / C ++ programming
(2) makefile preparation
(3) Linux command line
(4) GTEST background knowledge: it is used to do? It is what language? Some of its basic grammar? and many more
Above do not require knowledge of practice makes perfect, as long as the entry-level to reach, use GTEST not charge any effort, after all, is not a high threshold GTEST tool. Closer to home, we have to follow the steps one by one to explain:
Step 1: Download GTEST
Download link is: https: //code.google.com/p/googletest/downloads/list
The latest version is GTEST gtest-1.7.0.zip, so we will in this paper as an example to explain this version.
Step 2: Understanding Folder
After you download and unzip, you will see a folder gtest-1.7.0
So many documents at first glance does little people scratching their heads. In fact, GTEST provided support for a number of different platforms, such as msvc folder is used in Microsoft Visual Studio, xcode folder for Mac Xcode, codegrear folder for Borland C ++ Builder, in a Linux environment, we It is to make use of the contents of the folder.
Step 3: remove unnecessary files
We've talked about in the previous step, a lot of documents to support different platforms, in order to keep it simple program to avoid confusion audition, we all do not need to delete all the files in this step, retaining only what we need. In fact, we only need four folders, as shown below. The rest of the file and the folder to delete all (this step is not necessary, but we got a little older programmer is concerned, does not allow a bunch of unused code in it ~).
Our program more refreshing.
Step 4: rewrite the Makefile
At this point, if you go to make a folder, through the command line $ make && ./sample1_unittest command, you can see the results of the test. However, if you open the Makefile look, you will find that this applies only to compile makefile sample1, if I add a test source document? But also to re-write makefile, too much trouble. Thus, in this step, we redrafting Makefile.
Back we talked about, now only four folders (include, make, samples, src), which now make only a file should be overwritten, it did not need to stay up this folder. Then, in Step 4, the first thing you have to do is to make the folder, along with the inside of the Makefile delete all ...... Then, enter the samples folder, create a file yourself, called Makefile
Then, open the Makefile file, write the following, as shown (do not put the figure line number is also written into the oh ~). This new Makefile is just now being rewritten us to remove from the Makefile, if you're curious, then you can compare the differences between them, which involves some makefile syntax and function, if not familiar, you may need to spend a few minute check data to understand them.
Note that the figure in the Makefile rewrite line 32, we compile the cpp file name suffix, and give examples of the original ending was to cc. Therefore, you need to do one thing, is to sample1.cc file name to sample1.cpp, the file name to the sample1_unittest.cc sample1_unittest.cpp, it's done.
Now we have three folders (include, src, samples), a program of our own tested on sample folder. This folder name looked relatively unhappy, you can change it to mycode or testcode, then GTEST root folder name gtest-1.7.0 can also be changed to the class mygtest to meet our perfectionist demand.
Now, enter the command line to compile perform operations: $ make && ./run_test, you can see the result
Step 5: Add your test function
To the above step, in fact, our test environment has been set up is completed. If you have a function you want to be a test, you can continue with Step 5.
Suppose we now have a test function sqrt.cpp and its headers sqrt.h, their content is as follows:
Test the role of this document is to calculate the square root of an arbitrary positive integer, algorithm complexity in log (n) level. Mycode above three files in the folder, and then make && ./run_test compile run, you can see the results:
Summary: After the complete environment to build, test each time a file xxx.cpp and its xxx.h file, maybe put into mycode folder, then write xxx_unittest.cpp test files, but also inside. Then to this directory with the command line, make && ./run_test it should be easier than most to open a lot of time to make it? To test, we only need three files into mycode, then enter the command line directory make && ./run_test to complete the test. The following figure is now my mycode the folder. xxx_unittest.cpp the file name is not fixed, taking into something it does not matter, but it is easier to identify which file is which function under test only.
Of course, Google Test is a very powerful tool, but using the above explain its most basic function, as well as to build a simple environment. In fact, the document is needed and that the remaining three files mentioned in Step 4 of the folder contents, proper rewrite, you can achieve more powerful. The wait will have the opportunity to write it.