Ubuntu uses Gtest Unit test framework

Source: Internet
Author: User
Tags gcd greatest common divisor

Ext.: http://ningning.today/2014/11/12/%E6%B5%8B%E8%AF%95%E5%BC%80%E5%8F%91/ubuntu%E4%BD%BF%E7%94%A8gtest%E5%8D%95 %e5%85%83%e6%b5%8b%e8%af%95%e6%a1%86%e6%9e%b6/

Recently contacted Gtest,google's Open source C + + unit Testing Framework. Tell me about the steps to use on Ubuntu.

Installing the Gtest Development package:

sudo apt-get install libgtest-dev

Note that this step is to install the source code to/usr/src/gtest, you need to build the makefile with CMake and then make the static library.

123456
sudo apt-get install cmake    #安装cmakecd /usr/src/gtestsudo cmake CMakeLists.txtsudo makesudo cp *.a/usr/lib/    #拷贝生成的库到/usr/lib/
Write a simple test code testgcd.cpp
123456789101112131415161718192021
//testgcd.cpp#include <gtest/gtest.h>intGCD (intAintb//Calculation greatest common divisor{return 0= = b? A:GCD (b, a% B); }test (Gcdtest, inttest) {expect_eq (1, GCD (2,5)); Expect_eq (2, GCD (2,5)); Expect_eq (2, GCD (2,4)); Expect_eq (3, GCD (6,9));}intMainintargcChar**ARGV) {testing::initgoogletest (&AMP;ARGC, argv);returnRun_all_tests ();}

Then compile with g++, and note the libraries to be linked (you can also build with CMake, which is just a simple example):

g++ testgcd.cpp -lgtest_main -lgtest -lpthread

Executed under

./a.out

Look at the results of the output:

123456789101112131415161718
[==========] Running1 TestFrom1 TestCase. [----------]Global TestEnvironment set- up. [----------]1 TestFrom gcdtest[RUN] Gcdtest.inttestTestgcd.cpp: One: Failurevalue OF:GCD (2,5)Actual: 1expected: 2[FAILED] Gcdtest.inttest (0MS) [----------]1 TestFrom Gcdtest (0Ms total) [----------]Global TestEnvironment tear- Down[==========]1 TestFrom1 TestCase ran. (0Ms total) [PASSED]0Tests. [FAILED]1 Test, listed below:[FAILED] Gcdtest.inttest1FAILEDTEST

The output is easier to read. For more documentation and use of Gtest, refer to the official manual.

Testing under Engineering

When the test project is more, the general separation of the header files and implementation files, and then can be built with CMake.

1234567891011121314151617181920212223242526272829
//!gcd.h#ifndef Gcd_h#define gcd_hintGCD (intAintb);#endif //!gcd.cpp#include "Gcd.h"intGCD (intAintb) {return 0= = b? A:GCD (b, a% B); }//!testgcd.cpp#include "Gcd.h"#include <gtest/gtest.h>TEST (Gcdtest, inttest) {expect_eq (1, GCD (2,5)); Expect_eq (2, GCD (2,5)); Expect_eq (2, GCD (2,4)); Expect_eq (3, GCD (6,9));}intMainintargcChar**ARGV) {testing::initgoogletest (&AMP;ARGC, argv);returnRun_all_tests ();}

Next Write a CMakeLists.txt: (Specific information refer to CMake's documentation)

123456
cmake_minimum_required 2.6)    # Locate Gtest    Find_package (Gtest REQUIRED) include_directories (${gtest_include_dirs})         libraryadd_executable (testgcd testgcd.cpp gcd.cpp)    target_link_libraries${ Gtest_libraries} pthread)

Perform:

123
CMake CMakeLists.txtmake./testgcd

You can see the same execution results.

Reference

Google C + + testing Framework
Getting started with Google Test (gtest) on Ubuntu
A Quick Introduction to the Google C + + testing Framework

Ubuntu uses Gtest Unit test framework

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.