Gtest Use simple Summary

Source: Internet
Author: User

Because of the need to read chromium, I would like to learn a little bit about the use of gtest, because unit_test through chromium is a way to understand WebKit chromium port. In fact, gtest related articles more, the following references in this article listed a number of representative articles, is to learn the use of gtest valuable information.

First look at the logical concept:

A test_program can contain multiple test_case, and a test_case can contain more than one test, and a test_program refers to a link unit that contains the main function, which is a test_ Program can contain only one main function, and multiple test files, which contain multiple test_case, and one test_case contains multiple test. As shown in the following illustration:


Gtest uses some macros to determine whether the unit being tested gets the desired result, and the macro is basically divided into assert_* and expect_*, the difference being that the assert_* failure terminates the continuation of this test, and the Expect_* macro can continue to execute the test. Compares whether a string is empty assert_streq (null, the_string)

If more than one test uses the same data, you can use a class to share the data that needs to be shared, which is a member variable of the class, and the mechanism [1] called fixture,fixture has the setup and teardown functions for initializing and destructor objects, Setup and teardown are called before each test execution, which is tested in the code. If you use fixture, the TEST_F macro is used instead of the test macro, and the name of the Test_case is the name of the fixture class (this place made a mistake at the beginning).

A link unit's test program has a main function, and the main function is usually written as follows:

int main (int argc, char **argv) {
  :: Testing::initgoogletest (&ARGC, argv);
  return run_all_tests ();
}

where the Run_all_test () function will run all the TESTS in a link unit (run_all_tests () runs all TESTS in your link unit), it is important to note that the main function must return Run_all The return value of _tests, otherwise it will go wrong, that is, return run_all_tests (), and run_all_tests () can only be called 1 times, otherwise it will cause threading problems.

Let's take a look at some specific examples, assuming I have four files: my_test.cc, my_test1.cc, my_test2.cc, my_class.h

my_test.cc

#include <gtest/gtest.h>
TEST (mytestcase, firsttest) {
  expect_eq (2,2);
}
TEST (Mytestcase, secondtest) {
  expect_eq (2,2);
}
int main (int argc, char **argv) {
  :: Testing::initgoogletest (&ARGC, argv);
  return run_all_tests ();
}
my_test1.cc
#include <gtest/gtest.h>
TEST (mytestcase, thirdtest) {
  assert_eq (3,3) << "3 is not equal to 4";
  char* p = NULL;
  Assert_streq (NULL, p);
}
TEST (Yourtestcase, firsttest) {
  expect_eq (3,3);
}
my_test2.cc
#include <gtest/gtest.h>
#include "my_class.h"
class Myclasstest:public:: testing::test {
 public :
  virtual void SetUp () {
    My_class_ = new MyClass (ten);
  }
  virtual void TearDown () {
    delete my_class_;
  }
  myclass* my_class_;
};
Test_f (Myclasstest, firsttest) {
  expect_eq (ten, My_class_->getx ());
  My_class_->setx (2);
}
Test_f (Myclasstest, secondtest) {
  expect_eq (ten, My_class_->getx ());
  My_class_->setx (2);
}
My_class.h
#include <stdio.h>
class MyClass {public
 :
  MyClass (int x): X_ (x) {}
  void SetX (int x) {
    x_ = x;< c5/>}
  int GetX () const {
    return x_;
  }
 Private:
  int x_;
};

The compile command is as follows:

g++ my_test.cc  my_test1.cc  my_test2.cc-lgtest-lpthread

Run results

[==========] Running 6 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 2 tests from Myclasstest
[RUN      ] Myclasstest.firsttest
[       OK] myclasstest.firsttest (0 ms) c6/>[RUN      ] myclasstest.secondtest
[       OK] myclasstest.secondtest (0 ms)
[----------] 2 tests from Myclasstest (0 ms Total)

[----------] 3 tests from Mytestcase
[RUN      ] Mytestcase.thirdtest
[       OK] Mytestcase.thirdtest (0 ms)
[RUN      ] Mytestcase.firsttest
[       OK] mytestcase.firsttest (0 ms) 
  [RUN      ] mytestcase.secondtest
[       OK] mytestcase.secondtest (0 ms)
[----------] 3 tests from Mytestcase (0 ms Total)

[----------] 1 test from yourtestcase
[RUN      ] Yourtestcase.firsttest
[       OK] Yourtestcase.firsttest (0 ms)
[----------] 1 test from Yourtestcase (0 ms Total)

[----------] Global test Enviro Nment Tear-down
[==========] 6 tests from 3 test cases ran. (0 ms Total)
[  PASSED  ] 6 tests.

Reference [1] describes the use of the most basic gtest, reference [4] gives some examples of gtest use, reference [5] gives more gtest methods of use (including: type testing, parameter testing, etc.)

Reference documents:

[1]http://code.google.com/p/googletest/wiki/primer

[2]https://docs.google.com/present/view?id=dfsbxvm5_0f5s4pvf9

[3]http://www.cnblogs.com/coderzh/archive/2009/04/06/1426755.html

[4]http://code.google.com/p/googletest/wiki/samples

[5]http://code.google.com/p/googletest/wiki/advancedguide

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.