Google Test(GTest)使用方法和源码解析

发布时间 : 星期二 文章Google Test(GTest)使用方法和源码解析更新完毕开始阅读

Google Test(GTest)使用方法和源码解析

在分析源码之前,我们先看一个例子。以《Google Test(GTest)使用方法和源码解析——概况 》一文中最后一个实例代码为基准,修改最后一个“局部测试”结果为错误。(转载请指明出于breaksoftware的csdn博客)[cpp] view plain copy print?class ListTest : public testing::Test { protected: virtual void SetUp() { _m_list[0] = 11; _m_list[1] = 12; _m_list[2] = 13; } int _m_list[3]; }; TEST_F(ListTest, FirstElement) { EXPECT_EQ(11, _m_list[0]); } TEST_F(ListTest, SecondElement) { EXPECT_EQ(12, _m_list[1]); } TEST_F(ListTest, ThirdElement) { EXPECT_EQ(0, _m_list[2]); } 然后我们观察其输出,从下面的结果我们可以分析出GTest帮我们统计了:有多少测试用例一个测试用例中有多少测试特例一个测试用例中有多少测试特例成功一个测试用例中有多少测试特例失败失败的原因、位置、期待结果、实际结果[plain] view plain copy print?Running main() from gtest_main.cc [==========] Running 3 tests from 1 test case. [----------] Global test environment set-up. [----------] 3 tests from ListTest [ RUN ] ListTest.FirstElement [ OK ]

ListTest.FirstElement (0 ms) [ RUN ] ListTest.SecondElement [ OK ] ListTest.SecondElement (0 ms) [ RUN ]

ListTest.ThirdElement ../samples/sample11_unittest.cc:86: Failure Expected: 0 To be equal to: _m_list[2] Which is: 13 [ FAILED ] ListTest.ThirdElement (0 ms) [----------] 3 tests from ListTest (0 ms total) [----------] Global test environment tear-down [==========] 3 tests from 1 test case ran. (0 ms total) [ PASSED ] 2 tests. [ FAILED ] 1 test, listed below: [ FAILED ] ListTest.ThirdElement 1 FAILED TEST 在《Google Test(GTest)使用方法和源码解析——自动调度机制分析》一文中,我们分析了,测试用例对象指针将保存在类UnitTestImpl中[cpp] view plain copy print?// The vector of TestCases in their original order. It owns the // elements in the vector. std::vector<TestCase*> test_cases_; 那么结果的统计,肯定也是针对这个vector变量的。实际也是如此,我们在代码中找到如下函数,从函数注释,我们就可以知道其对应于上面输出中那个结果的统计[cpp] view plain copy print?// Gets the number of successful test cases. int

UnitTestImpl::successful_test_case_count() const

{ return CountIf(test_cases_, TestCasePassed); } // Gets the number of failed test cases. int

UnitTestImpl::failed_test_case_count() const { return CountIf(test_cases_, TestCaseFailed); } // Gets the number of all test cases. int

UnitTestImpl::total_test_case_count() const { return static_cast<int>(test_cases_.size()); } // Gets the number of all test cases that contain at least one test // that should run. int

UnitTestImpl::test_case_to_run_count() const { return CountIf(test_cases_, ShouldRunTestCase); } // Gets the number of successful tests. int

UnitTestImpl::successful_test_count() const { return SumOverTestCaseList(test_cases_,

&TestCase::successful_test_count); } // Gets the number of failed tests. int

UnitTestImpl::failed_test_count() const { return SumOverTestCaseList(test_cases_,

&TestCase::failed_test_count); } // Gets the number of disabled tests that will be reported in the XML report. int UnitTestImpl::reportable_disabled_test_count()

const { return SumOverTestCaseList(test_cases_,

&TestCase::reportable_disabled_test_count); } // Gets the number of disabled tests. int

UnitTestImpl::disabled_test_count() const { return SumOverTestCaseList(test_cases_,

&TestCase::disabled_test_count); } // Gets the number of tests to be printed in the XML report. int UnitTestImpl::reportable_test_count() const { return SumOverTestCaseList(test_cases_,

&TestCase::reportable_test_count); } // Gets the number of all tests. int UnitTestImpl::total_test_count() const { return SumOverTestCaseList(test_cases_, &TestCase::total_test_count); } // Gets the number of tests that should run. int

UnitTestImpl::test_to_run_count() const { return SumOverTestCaseList(test_cases_,

&TestCase::test_to_run_count); } CountIf函数返回符合条件的测试用例个数,SumOverTestCaseList函数返回符合条件的所有测试特例的个数。其实现也非常简单,我们以CountIf为例

[cpp] view plain copy print?template <class Container, typename Predicate> inline int CountIf(const Container& c, Predicate predicate) { //

联系合同范文客服:xxxxx#qq.com(#替换为@)