
- 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 - Event Listeners
The event listener in GoogleTest is an API that let you receive notifications about the progress of a test program and its success and failures. Using this API, you can get notified for the various events such as start and end of the test program, a test suite, or a test method, among others.
Additionally, it also helps you to replace the standard console output to XML output, or provide a completely different format of output, such as a GUI or a database.
Each event in GoogleTest is associated with an handler function. When an event is fired, its context is passed to these functions as an argument. The argument types are −
- UnitTest − It represents the state of the entire test program.
- TestSuite − It provides information about a test suite.
- TestInfo − It contains the state of a single test.
- TestPartResult − It represents the result of a test assertion.
Defining Event Listeners
In GoogleTest, an event listener can be defined by inheriting either testing::TestEventListener interface or testing::EmptyTestEventListener interface.
TestEventListener Interface
The TestEventListener interface is used to track execution of tests. It provides the below listed virtual methods that can be overridden to handle a test event −
- OnTestProgramStart − This method is fired prior to the start of any activity.
- OnTestIterationStart − It is fired before each iteration of tests starts.
- OnEnvironmentsSetUpStart − Fired before environment set-up for each iteration of tests starts.
- OnEnvironmentsSetUpEnd − Fired after environment set-up for each iteration of tests ends.
- OnTestSuiteStart − Called before the starting of a test suite.
- OnTestEnd − It is fired when the specified test ends.
EmptyTestEventListener Interface
The EmptyTestEventListener interface in GoogleTest provides an empty implementation of all methods in the TestEventListener interface. To use this, you simply need to override the methods.
How to Use Defined Event Listeners
To use the event listener you have defined, add an instance of it to the GoogleTest event listener list in your main() function, before calling RUN_ALL_TESTS(). This event listener list is represented by TestEventListeners class.
Example
Let's see how to write event listeners in GoogleTest.
#include <gtest/gtest.h> #include <iostream> class FirstTestEventListener : public ::testing::EmptyTestEventListener { // Called before a test starts void OnTestStart(const ::testing::TestInfo& test_info) override { std::cout << "test is starting : " << test_info.name() << std::endl; } // Called after a test ends void OnTestEnd(const ::testing::TestInfo& test_info) override { std::cout << "test finished : " << test_info.name() << std::endl; } }; int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); // Create and register the custom test event listener ::testing::TestEventListeners& listeners = ::testing::UnitTest::GetInstance() -> listeners(); listeners.Append(new FirstTestEventListener); int result = RUN_ALL_TESTS(); }
This code will not produce any output as we haven't given any test.