
- 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 - Test Fixtures
Test Fixtures in GoogleTest are used when you need to write two or more tests that works on similar data. It allows developers to reuse the same data configuration for several different tests. The fixture class is derived from the ::testing::Test class available in the gtest.h header file.
How to Create Test Fixtures?
Follow the steps given below to create test fixtures in GoogleTest −
- Create a class that derives from testing::Test class. Its body should be protected.
- Declare objects you want to use inside this class.
- Then, define setup logic which is placed inside the constructor or SetUp() function. It will prepare the objects for each test.
- To release any resources you allocated using SetUp(), write a destructor or TearDown() function. It is optional.
- At last, define subroutines for your tests to share.
Syntax
Syntax to create test fixture is as follows −
// Define a test fixture class class FirstTestFixture : public ::testing::Test { protected: // SetUp() function void SetUp() override { // setup code comes here } // TearDown() function void TearDown() override { // teardown code comes here } // define common data members for the tests data_type variable = value; };
Role of TEST_F() Macro in Test Fixtures
The TEST_F() macro in GoogleTest is used to define a test that uses the test fixture class. The _F here stands for Fixture. It accepts two parameters which are test name and name of the test fixture class. Both arguments must be valid C++ identifiers and must not contain underscores (_).
Syntax
Let's see the syntax of TEST_F macro −
TEST_F(TestFixtureName, TestName) { ... code to be tested ... }
Generally, TEST() macro is used to define a test in GoogleTest. However, TEST_F() macro is used while working with test fixtures as it allows you to access objects and subroutines defined in that particular test fixture. You can observe that the syntax of TEST_F() macro where name of a test fixture class is declared instead of test suite name.
For each test defined with TEST_F(), GoogleTest creates a new test fixture at runtime, initializes it using SetUp() function, runs the test, cleans up by calling TearDown() function, and then deletes the test fixture.
Any changes made to the one test in fixture do not affect other tests because different tests in the same test suite have different test fixture objects. Also, GoogleTest always deletes a test fixture before it creates the next one.
Example
The following example illustrates how to write test fixtures using TEST_F() in GoogleTest.
#include <gtest/gtest.h> // test fixture class class FixtureDemo : public ::testing::Test { protected: int* test_var; // SetUp() function void SetUp() override { // dynamically allocating an integer test_var = new int(42); } // TearDown Function void TearDown() override { // deallocate the integer delete test_var; test_var = nullptr; } }; // to verify equality TEST_F(FixtureDemo, ForEquals) { EXPECT_EQ(42, *test_var); } // to verify less than TEST_F(FixtureDemo, ForLessThan) { EXPECT_LT(42, *test_var); } // main function int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
When you run this code, following output will be displayed on console −
Test project D:/gTest/test6/build Start 1: FixtureDemo.ForEquals 1/2 Test #1: FixtureDemo.ForEquals ............ Passed 0.03 sec Start 2: FixtureDemo.ForLessThan 2/2 Test #2: FixtureDemo.ForLessThan ..........***Failed 0.02 sec 50% tests passed, 1 tests failed out of 2 Total Test time (real) = 0.13 sec The following tests FAILED: 2 - FixtureDemo.ForLessThan (Failed)