
- 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 - Disabling Test
A good testing framework should have the feature of disabling a test temporarily. Let's say there is a test which is failing due to a bug and it is blocking the other tests. In this situation, only one solution would be possible and that is disabling the test so that other tests continue to run. It is also helpful where a feature is still under development and not ready for testing.
In GoogleTest, you can disable a test either by using DISABLED_ prefix or using GTEST_SKIP() Macro. GoogleTest is a testing framework used to test C++ code.
Using DISABLED_ Prefix
A test can be marked disabled by changing its name to DISABLED_*, where, "*" represents name of a test. By doing this GoogleTest will skip the specified test when you run the test binary. However, the test will still be compiled.
Syntax
Syntax of the DISABLED_ prefix is as follows −
DISABLED_TestName
Example
In this code, we disable a test in GoogleTest using DISABLED_ prefix.
#include <gtest/gtest.h> // function to be tested int divideNum(int nums1, int nums2) { if (nums2 == 0) { throw std::invalid_argument("Denominator cannot be zero"); } return nums1 / nums2; } // test 1 TEST(CheckDivison, ForPositiveInput) { EXPECT_EQ(12, divideNum(24, 2)); } // test 2 TEST(CheckDivison, ForNegativeInput) { EXPECT_EQ(-2, divideNum(4, -2)); EXPECT_EQ(-2, divideNum(-4, 2)); EXPECT_EQ(2, divideNum(-4, -2)); } // test 3 TEST(DISABLED_CheckDivison, ForZeroDenominator) { EXPECT_THROW(divideNum(4, 0), std::invalid_argument); } // main() function to run the test int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
On running, output will be displayed as follows −
Test project D:/gTest/test2/build Start 1: CheckDivison.ForPositiveInput 1/3 Test #1: CheckDivison.ForPositiveInput ..... Passed 0.02 sec Start 2: CheckDivison.ForNegativeInput 2/3 Test #2: CheckDivison.ForNegativeInput ..... Passed 0.02 sec Start 3: CheckDivison.ForZeroDenominator 3/3 Test #3: CheckDivison.ForZeroDenominator ...***Not Run (Disabled) 0.00 sec 100% tests passed, 0 tests failed out of 2 Total Test time (real) = 0.13 sec The following tests did not run: 3 - CheckDivison.ForZeroDenominator (Disabled)
Using GTEST_SKIP() Macro
The GTEST_SKIP() macro is a newer feature added in GoogleTest with the release of version 1.10.0. It is used to skip a test at runtime based on the given condition.
Syntax
Syntax of the GTEST_SKIP() macro is given below −
GTEST_SKIP() << "Text to display";
Example
Following code shows how to use the GTEST_SKIP() to disable a test in GoogleTest −
#include <gtest/gtest.h> // function to be tested int prod(int num1, int num2) { return num1 * num2; } // to disable test bool disableTest(int num1, int num2) { if(num1 == num2) { return true; } else { return false; } } // Test suite TEST(ProductTest, ForEqualNumbers) { int num1 = 3; int num2 = 3; if (disableTest(num1, num2)) { GTEST_SKIP() << "Disabling test"; } ASSERT_EQ(prod(num1, num2), 9); } int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); }
Output of the above code is as follows −
Test project D:/gTest/test3/build Start 1: ProductTest.ForEqualNumbers 1/1 Test #1: ProductTest.ForEqualNumbers ......***Skipped 0.03 sec 100% tests passed, 0 tests failed out of 1 Total Test time (real) = 0.10 sec The following tests did not run: 1 - ProductTest.ForEqualNumbers (Skipped)