GoogleTest、GoogleMock学习总结

1 源码

GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework

2 编译构建

googletest/googletest/README.md

GoogleTest、GoogleMock学习总结_第1张图片

3 googletest自带的测试代码

3.1 googletest/googletest/samples/

CMakeLists里打开gtest_build_samples,googletest/docs/samples.md:

Sample #1 shows the basic steps of using googletest to test C++ functions.

基本函数 TEST,适用于基本函数的简单测试

Sample #2 shows a more complex unit test for a class with multiple member functions.

类 TEST,适用于类的简单测试

Sample #3 uses a test fixture.

测试套件 TEST_F,适用于{#same-data-multiple-tests}

Sample #4 teaches you how to use googletest and googletest.h together to get the best of both libraries.

Sample #5 puts shared testing logic in a base test fixture, and reuses it in derived fixtures.

超级测试套件 TEST_F,适用于测试固件作为父类派生出测试子固件,把测试子固件中的共有特性抽取出来组成超级固件

Sample #6 demonstrates type-parameterized tests.

类型/类型参数化测试 TEST_P,适用于同一个接口的不同实现类的测试

Sample #7 teaches the basics of value-parameterized tests.

值参数化 TEST_P,适用于同一个函数的不同参数

Sample #8 shows using Combine() in value-parameterized tests.

值参数化+Conbine() TEST_P,适用于同一个函数的不同参数

Sample #9 shows use of the listener API to modify Google Test's console output and the use of

its reflection API to inspect test results.

Sample #10 shows use of the listener API to implement a primitive memory leak checker.

适用于事件监听API

3.2 googletest/googletest/test/

CMakeLists里打开gtest_build_tests

4 googletest基本用法

googletest/docs/primer.md

4.1 Basic Concepts

测试工程,测试套和测试用例的关系:

GoogleTest、GoogleMock学习总结_第2张图片

4.2 Assertions

1 Basic Assertions

GoogleTest、GoogleMock学习总结_第3张图片

2 Binary Comparison

GoogleTest、GoogleMock学习总结_第4张图片

3 String Comparison

GoogleTest、GoogleMock学习总结_第5张图片

4.3 Simple Tests 

sample1_unittest,sample2_unittest 

4.4 Test Fixtures

Using the Same Data Configuration for Multiple Tests {#same-data-multiple-tests}

sample3_unittest  

5 googletest进阶用法

googletest/docs/advanced.md

5.1 More Assertions

1 Exception Assertions

Fatal assertion Nonfatal assertion Verifies
ASSERT_THROW(statement, exception_type); EXPECT_THROW(statement, exception_type); statement throws an exception of the given type
ASSERT_ANY_THROW(statement); EXPECT_ANY_THROW(statement); statement throws an exception of any type
ASSERT_NO_THROW(statement); EXPECT_NO_THROW(statement); statement doesn't throw any exception

2 Floating-Point Comparison

Fatal assertion Nonfatal assertion Verifies
ASSERT_FLOAT_EQ(val1, val2); EXPECT_FLOAT_EQ(val1, val2); the two float values are almost equal
ASSERT_DOUBLE_EQ(val1, val2); EXPECT_DOUBLE_EQ(val1, val2); the two double values are almost equal

5.2 Teaching googletest How to Print Your Values 

5.3 Death Tests

“死亡测试”名字比较恐怖,这里的“死亡”指的的是程序的崩溃。通常在测试过程中,我们需要考虑各种各样的输入,有的输入可能直接导致程序崩溃,这时我们就需要检查程序是否按照预期的方式挂掉,这也就是所谓的“死亡测试”。gtest的死亡测试能做到在一个安全的环境下执行崩溃的测试案例,同时又对崩溃结果进行验证。

5.4 Using Assertions in Sub-routines

Adding Traces to Assertions

5.5 Sharing Resources Between Tests in the Same Test Suite

So, in addition to per-test set-up/tear-down, googletest also supports per-test-suite set-up/tear-down.

static void SetUpTestSuite();
static void TearDownTestSuite();

5.6 Global Set-Up and Tear-Down

GoogleTest、GoogleMock学习总结_第6张图片

5.7 Value-Parameterized Tests

Value-parameterized tests allow you to test your code with different parameters without writing multiple copies of the same test. 

1 To write value-parameterized tests, first you should define a fixture class.

GoogleTest、GoogleMock学习总结_第7张图片

2 Then, use the TEST_P macro to define as many test patterns using this fixture as you want. 

GoogleTest、GoogleMock学习总结_第8张图片

3 Finally, you can use the INSTANTIATE_TEST_SUITE_P macro to instantiate the test suite with any set of parameters you want.  

sample7_unittest,sample8_unittest 

5.8 Typed Tests

sample6_unittest

5.9 Type-Parameterized Tests

Type-parameterized tests are like typed tests, except that they don't require you to know the list of types ahead of time. Instead, you can define the test logic first and instantiate it with different type lists later. You can even instantiate it more than once in the same program.

sample6_unittest

5.10 Testing Private Code

#define private public

上面方法最简单,还介绍了其他的,比如友元...

5.11 Getting the Current Test's Name

GoogleTest、GoogleMock学习总结_第9张图片

5.12 Extending googletest by Handling Test Events

事件监听API 

sample9_unittest

5.13 Running Test Programs: Advanced Options

1 Selecting Tests

(1)Listing Test Names

--gtest_list_tests

(2)Running a Subset of the Tests

--gtest_filter

(3)Stop test execution upon first failure

--gtest_fail_fast

(4)Temporarily Disabling Tests

(5)Temporarily Enabling Disabled Tests

2 Repeating the Tests

--gtest_repeat

3 Shuffling the Tests

随机测试

4 Controlling Test Output

(1)Colored Terminal Output

--gtest_color

(2)Suppressing test passes

--gtest_brief=1

(3)Suppressing the Elapsed Time

--gtest_print_time=0

(4)Suppressing UTF-8 Text Output

--gtest_print_utf8=0

(5)Generating an XML Report

--gtest_output xml:path_to_output_file

(6)Generating a JSON Report

--gtest_output json:path_to_output_file

5 Controlling How Failures Are Reported

6 googlemock自带例子

googletest/googlemock/test

CMakeLists里打开gmock_build_tests

7 googlemock基本用法

googletest/docs/gmock_for_dummies.md

Turtle_unittest

1 Getting Started

2 Setting Expectations

3 Matchers: What Arguments Do We Expect?

4 Actions: What Should It Do?

5 Using Multiple Expectations {#MultiExpectations}

6 Ordered vs Unordered Calls {#OrderedCalls}

7 All Expectations Are Sticky (Unless Said Otherwise) {#StickyExpectations}

8 googlemock进阶用法

googletest/docs/gmock_cook_book.md

9 参考资料

GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework

gtest实现架构简单分析_做一个有技术追求的人-CSDN博客_gtest原理

gtest简介及简单使用_网络资源是无限的-CSDN博客_gtest

基于 Gtest 的单元测试入门及实践 (一) · TesterHome

gmock使用、原理及源码分析 - 傲衣华少 - 博客园

gmock简单实例_chent86的博客-CSDN博客_gmock示例

Gtest/Gmock探究(一)-- 经典示例代码_Mr.H的专栏-CSDN博客_gmock示例

你可能感兴趣的:(#,[C++],GoogleTest,googletest)