
- GoogleTest - Home
- GoogleTest - Overview
- GoogleTest - Environment Setup
- GoogleTest - Running First Test
- GoogleTest - Nomenclature
- GoogleTest - Disabling Test
- GoogleTest - Assertions
- GoogleTest - Death Tests
- GoogleTest - Test Fixtures
- GoogleTest - Event Listeners
GoogleTest Useful Resources
GoogleTest - Overview
GoogleTest or gtest is a testing framework developed by Google. It is mainly used for unit testing of C++ programs. It is based on the xUnit architecture, which is a widely adopted framework for writing and executing automated tests. This tutorial explains the basics as well as advanced topics related to GoogleTest such as its nomenclature, assertions, test fixtures and much more.
Purpose of GoogleTest
Focus on Unit Testing − GoogleTest primarily focuses on unit testing of the component of an application. A unit of code is to be tested in isolation to check if it is functioning as per the requirements.
Improved Code Quality − GoogleTest provides a structured way to write unit tests. Makes it easy for developers to catch bugs early in developement stage and allows refactoring of code with confidence.
Core Concepts of GoogleTest
-
Assertions − GoogleTest provides assertions to check success or failure conditions. Following are important assertions −
ASSERT_* macros − Used to identify fatal failures. If an ASSERT_ fails, current function test will abort immidiately.
EXPECT_* macros − Used to identify non-fatal failures. If an EXPECT_ fails, current function test continues and allow to find further failures.
Examples − ASSERT_TRUE(condition), ASSERT_NEAR(value1, value2, abs_error), EXPECT_EQ(expected, actual), EXPECT_NE(value1, value2)
-
TEST() macro − defines a function contains assertions to verify a behavior.
Syntax
TEST(TestSuiteName, TestName)
Where TestSuiteName is name of the group carrying tests and TestName is a unique test name with the test suite.
-
TEST_F() macro − Used to define a test fixture. When we need to perform multiple tests on same object, we can define and use test fixtures.
Syntax
TEST_F(FixtureName, TestName)
Test Suites − Test suites is a set of related tests often sharing common setup logic using test fixtures.
Automatic Test Discovery − gtest automatically registers the test cases defined in test executables and runs them. Manually listing is not required.
Key Features of GoogleTest
Platform Independent − gtest works seemlessly on all major platforms including linux, macOS and windows and different types of compilers.
Informative Failure Messages − gtest produces informations like file name, line number, values used in assertions when test cases fails. This helps greatly while debugging the failed test cases.
Terminal Tests − In gtest, we can write test cases which if failed, terminates the process using ASSERT_* macros.
Value-Parameterized Tests −gtest allows to pass different values to same test test making it easier to test different values of inputs.
Type-Parameterized Tests − In case of templates testing, gtest type passing feature can be used to create type parameterized tests.
XML Test Report Generation − gtest test reports can be generated in XML Formats making integration easy with Continous Integration,CI systems.
Integration with Google Mock (gmock) − gtest is often used in conjunction with gmock and provides a seemless integration.
Command-Line Arguments − gtest comes with various command-line flags to control test execution like filtering the tests, repeating tests, breaking on failure, output results and so on.