GitHub - google/googletest: GoogleTest - Google Testing and Mocking Framework
googletest/googletest/README.md
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
CMakeLists里打开gtest_build_tests
googletest/docs/primer.md
测试工程,测试套和测试用例的关系:
1 Basic Assertions
2 Binary Comparison
3 String Comparison
sample1_unittest,sample2_unittest
Using the Same Data Configuration for Multiple Tests {#same-data-multiple-tests}
sample3_unittest
googletest/docs/advanced.md
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 |
“死亡测试”名字比较恐怖,这里的“死亡”指的的是程序的崩溃。通常在测试过程中,我们需要考虑各种各样的输入,有的输入可能直接导致程序崩溃,这时我们就需要检查程序是否按照预期的方式挂掉,这也就是所谓的“死亡测试”。gtest的死亡测试能做到在一个安全的环境下执行崩溃的测试案例,同时又对崩溃结果进行验证。
Adding Traces to Assertions
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();
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.
2 Then, use the TEST_P macro to define as many test patterns using this fixture as you want.
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
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
#define private public
上面方法最简单,还介绍了其他的,比如友元...
事件监听API
sample9_unittest
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
googletest/googlemock/test
CMakeLists里打开gmock_build_tests
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}
googletest/docs/gmock_cook_book.md
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示例