Introduction
gtest
is a unit testing framework for C++. It provides a simple and straightforward way to write unit tests, and it also provides a number of features that make it easy to write effective unit tests, such as assertions, fixtures, and death tests.gmock
is a mocking framework for C++. It allows you to create mock objects that can be used to simulate the behavior of real objects in unit tests. This can be useful for testing the interaction between different parts of your code, or for testing code that depends on external resources that are not available in unit tests.
Here is a brief overview of how to use gtest and gmock together:
- Import the necessary headers.
- Create a mock object.
- Set expectations on the mock object.
- Exercise the code that uses the mock object.
- Verify that the expectations on the mock object were met.
Example
Here is an example of how to use gtest and gmock together:
#include <gtest/gtest.h>
#include <gmock/gmock.h>
class MockFoo {
public:
MOCK_METHOD(int GetSize(), (), (const));
};
TEST(FooTest, GetSize) {
MockFoo foo;
EXPECT_CALL(foo, GetSize()).WillOnce(Return(10));
int size = foo.GetSize();
EXPECT_EQ(size, 10);
}
In this example, we create a mock object of type MockFoo. We then set an expectation on the GetSize() method of the mock object, specifying that it should be called once and should return the value 10. We then exercise the code that uses the mock object, calling the GetSize() method. Finally, we verify that the expectation on the mock object was met, by checking that the value returned by GetSize() is equal to 10.
Macros
gtest
- ASSERT_EQ(expected, actual): Asserts that the two values are equal.
- ASSERT_NE(expected, actual): Asserts that the two values are not equal.
- ASSERT_TRUE(condition): Asserts that the condition is true.
- ASSERT_FALSE(condition): Asserts that the condition is false.
- FAIL(): Fails the current test.
- SUCCEED(): Succeeds the current test.
gmock
- EXPECT_CALL(mock_object, method_name(…)): Sets an expectation on the method_name method of the mock_object mock object.
- WillOnce(return_value): Specifies that the method should be called once and should return the return_value.
- WillRepeatedly(return_value): Specifies that the method can be called any number of times and should always return the return_value.
- WillOnce(Throw(exception_type)): Specifies that the method should be called once and should throw an exception of type exception_type.
- WillRepeatedly(Throw(exception_type)): Specifies that the method can be called any number of times and should always throw an exception of type exception_type.
Test
- TEST : Defines a standalone test case.
- TEST_F : Defines a test case that uses a test fixture.
- TEST_P : Defines a parameterized test case.
- INSTANTIATE_TEST_SUITE_P : Instantiates a parameterized test suite.
The TEST macro is the most basic test macro. It defines a standalone test case that does not use a test fixture. The TEST_F macro defines a test case that uses a test fixture. A test fixture is a set of data or objects that you want to use across multiple tests. The TEST_P macro defines a parameterized test case. A parameterized test case is a test case that can be run with different parameters. The INSTANTIATE_TEST_SUITE_P macro instantiates a parameterized test suite.
The choice of which test macro to use depends on the specific testing needs and requirements of your project.
Here are a few more:
- TEST_DISABLED : Defines a test case that is disabled.
- TEST_MAIN : Defines the main function for a gtest-based test program.
- TEST_WITH_MAIN : Defines a test program that has a main function.
- TYPED_TEST : Defines a typed test case.
- TYPED_TEST_F : Defines a typed test case that uses a test fixture.
- TYPED_TEST_P : Defines a typed parameterized test case.
- INSTANTIATE_TYPED_TEST_SUITE_P : Instantiates a typed parameterized test suite.
For more information on how to use gtest and gmock, please refer to the official documentation: https://google.github.io/googletest/.