What it is
Google Test (gtest) is a robust C++ unit testing framework developed by Google. It provides a rich set of assertions, test fixtures, parameterized tests, and utilities to help write reliable and maintainable tests.
Google Test allows writing unit tests using `TEST` and `TEST_F` macros. It provides assertions like `EXPECT_EQ`, `ASSERT_TRUE`, `EXPECT_THROW`, supports test fixtures for shared setup/teardown, and parameterized tests for testing multiple inputs.
- Licence
- BSD 3-clause
- Best known for
- GoogleMock — the most capable C++ mocking framework
When to use it
The question documentation cannot answer for you — because it cannot recommend something else.
Reach for it when
- Large codebases needing fixtures, parameterised tests and death tests
- You need proper mocking of C++ interfaces
Look elsewhere when
- A small project where Catch2's simpler setup would do
Installation
sudo apt install libgtest-dev && cd /usr/src/gtest && sudo cmake . && sudo make && sudo cp *.a /usr/libGetting started
The smallest useful thing you can do with it, and what each part means.
#include <gtest/gtest.h>
int Add(int a, int b) {
return a + b;
}
TEST(MathTest, AddPositiveNumbers) {
EXPECT_EQ(Add(2, 3), 5);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}TEST(MathTest, DivideTest) {
int x = 10;
int y = 2;
ASSERT_NE(y, 0); // stops test if y is zero
EXPECT_EQ(x / y, 5); // continues even if this fails
}Advanced usage
Where the library earns its place over a simpler alternative.
class MyTest : public ::testing::Test {
protected:
void SetUp() override {
value = 10;
}
int value;
};
TEST_F(MyTest, CheckValue) {
EXPECT_EQ(value, 10);
}class FactorialTest : public ::testing::TestWithParam<std::pair<int,int>> {};
TEST_P(FactorialTest, HandlesFactorial) {
auto [input, expected] = GetParam();
EXPECT_EQ(Factorial(input), expected);
}
INSTANTIATE_TEST_SUITE_P(FactorialTests, FactorialTest, ::testing::Values(
std::make_pair(0,1), std::make_pair(1,1), std::make_pair(3,6)
));TEST(ExceptionTest, ThrowsWhenNegative) {
EXPECT_THROW(FunctionThatThrows(-1), std::invalid_argument);
}Errors and fixes
The failures you are most likely to hit, and what actually resolves them.
- Test failure
- Review the assertion message. Use `ASSERT_` or `EXPECT_` appropriately and check the input data or function logic.
- Segmentation fault in test
- Ensure proper initialization of objects and valid memory usage inside tests.
Best practices
- Use `EXPECT_` for non-critical checks and `ASSERT_` for critical preconditions.
- Organize tests into logical test suites using `TEST` and `TEST_F`.
- Keep tests small, focused, and independent from each other.
- Use parameterized tests to cover multiple scenarios efficiently.
- Integrate Google Test with CI/CD pipelines for automated testing.
Alternatives
Comparable options, and the reason you would pick one over the other.
Background
Why it exists, and what it was reacting to.
Google Test was created by Google engineers to provide a standardized and efficient way to write C++ unit tests. It supports both small-scale and large-scale projects, integrating well with CI pipelines and offering detailed test reporting.
