Basic use of gtest under Mac

Source: Internet
Author: User

Basic use of gtest under Mac

Gtest's full name is the Google C + + testing Framework, which is a very good test framework that was born inside Google and is sought after by the industry, supporting features such as automated discovery testing, custom assertions, death tests, automated reporting, and more.

This document records the installation configuration gtest and its basic application in Mac

First, installation

Bo Master try to use brew install encountered problems, so direct source installation

Compiling the installation

 git clone https://github.com/google/googletest cd googletest mkdir build && cd build cmake .. make make install

Test code

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)set(CMAKE_CXX_STANDARD 11)project(demo)find_package(GTEST REQUIRED)add_executable(${PROJECT_NAME} main.cpp)target_link_libraries(${PROJECT_NAME} ${GTEST_LIBRARIES})

Main.cpp

#include <iostream>#include <gtest/gtest.h>int add(int a, int b) {    return a + b;}int sub(int a, int b) {    return a - b;}// case1TEST(test, c1) {    EXPECT_EQ(3, add(1, 2));    EXPECT_EQ(12, add(2, 6));}// case2TEST(test, c2) {    EXPECT_EQ(-1, sub(1, 2));}GTEST_API_ int main(int argc, char ** argv) {    testing::InitGoogleTest(&argc, argv);    return RUN_ALL_TESTS();}

Compile run

cmake .make./demo# 输出?  build ./demo[==========] Running 2 tests from 1 test case.[----------] Global test environment set-up.[----------] 2 tests from test[ RUN      ] test.c1/Users/fanghao/Desktop/test/main.cpp:15: FailureExpected equality of these values:  12  add(2, 6)    Which is: 8[  FAILED  ] test.c1 (0 ms)[ RUN      ] test.c2[       OK ] test.c2 (0 ms)[----------] 2 tests from test (0 ms total)[----------] Global test environment tear-down[==========] 2 tests from 1 test case ran. (0 ms total)[  PASSED  ] 1 test.[  FAILED  ] 1 test, listed below:[  FAILED  ] test.c1 1 FAILED TEST
Ii. Basic Concepts
  • Assertion: That is assertion, check whether the condition is true, the result of an assertion is

    • sucess
    • Nonfatal failure (non-fatal failure)
    • Fatal failure (fatal failure)

    Note Here is the next two, nonfatal failure will point out the error, and then continue to run, generated by EXPECT_XX, fatal failure will indicate the error but terminate the current test function, generated by the ASSERT_XX, generally recommend the former, You can find more than one problem at a time

  • Testing Sample: Test case represents the object to be tested, the general test will have two parameters, namely the test sample, and the test sub-case, such as the following test factorial sample, Both Handleszeroinput and Handlespositiveinput belong to the Factorialtest test sample.

    // Tests factorial of 0TEST(FactorialTest, HandlesZeroInput) {EXPECT_EQ(1, Factorial(0));}// Tests factorial of positive numbers.TEST(FactorialTest, HandlesPositiveInput) {EXPECT_EQ(1, Factorial(1));EXPECT_EQ(2, Factorial(2));EXPECT_EQ(6, Factorial(3));EXPECT_EQ(40320, Factorial(8));}
  • Common assertions

    Fatal Assertion Nonfatal Assertion verifies
    Assert_true (condition); Expect_true (condition); Condition is true
    Assert_false (condition); Expect_false (condition); Condition is False
    Assert_eq (expected, actual); Expect_eq (expected, actual); expected = = Actual
    Assert_ne (Val1, val2); Expect_ne (Val1, val2); Val1! = Val2
    Assert_lt (Val1, val2); Expect_lt (Val1, val2); Val1 < Val2
    Assert_le (Val1, val2); Expect_le (Val1, val2); Val1 <= Val2
    ASSERT_GT (Val1, val2); EXPECT_GT (Val1, val2); Val1 > Val2
    Assert_ge (Val1, val2); Expect_ge (Val1, val2); Val1 >= Val2
    Assert_streq (Expected_str, ACTUAL_STR); Expect_streq (Expected_str, ACTUAL_STR); The C strings has the same content
    Assert_strne (str1, str2); Expect_strne (STR1,STR2); The C strings has different content
    Assert_strcaseeq (Expected_str, ACTUAL_STR); Expect_strcaseeq (Expected_str, ACTUAL_STR); The C strings has the same Content,ignore case
    Assert_strcasene (str1, str2); Expect_strcasene (STR1,STR2); The C strings has different content,ignore case
Third, Test Fixture

Test fixture is the encapsulation of the basic test case, and if more than one test needs to manipulate similar data, you can encapsulate them in a class and share some configuration.

Writing fixure steps

    1. Create a class and inherit:: Testing::test, and use the protected or public limiter so that its subclasses can access the shared data
    2. In this class, declare the object you want to reuse.
    3. Write a setup function to prepare the desired object
    4. Write a teardown function to free up resources
    5. Other usages, like the basic test case, simply use the Test_f () macro instead of the test () macro, and each call to a test_f will rerun Setup and teardown, but the member objects can share

Look at an example that should be more descriptive.

#include <iostream> #include <gtest/gtest.h>class foo{public:int Add (int a, int b) {return a +    b    } int sub (int a, int b) {return a-A; }};class footest:public:: testing::test{protected:foo Foo;//Shared object/SetUp && TearDown would b E called for every Test case virtual void SetUp () {std::cout << ' Code here'll be called immediately    After the constructor "<< Std::endl; } virtual void TearDown () {std::cout << "Code here'll be called immediately after each test" <&lt ;    Std::endl; }};//Test Case 1test_f (footest, Testmethodadd) {expect_eq (3, Foo.add (1, 2));} Test Case 2test_f (footest, testmethodsub) {expect_eq ( -1, Foo.sub (1, 2));}    int main (int argc, char *argv[]) {:: Testing::initgoogletest (&AMP;ARGC, argv); return run_all_tests ();}  Output? Build./demo[==========] Running 2 Tests from 1 test case. [----------] Global test environment set-up. [----------] 2 tests from footest[RUN] Footest.testmethodaddcode here'll be called immediately after the Constructorcode he Re'll be called immediately after each test[OK] Footest.testmethodadd (0 ms) [RUN] Footest.testmethodsubco De here'll be called immediately after the Constructorcode here'll be called immediately all test[OK] F Ootest.testmethodsub (0 ms) [----------] 2 tests from Footest (0 ms total) [----------] Global test environment tear-down[== ========] 2 Tests from 1 test case ran. (1 ms Total) [PASSED] 2 tests.
Iv. Summary

Feel gtest is still very elegant, suitable for TDD mode, the test and development completely decoupled.

Basic use of gtest under Mac

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.