GoogleTest - Nomenclature



There are several terms related to GoogleTest that might confuse its users especially Test, Test Case and Test Suite. Meaning of these terms can differ from other testing frameworks. In this tutorial, we will understand them to avoid any confusion that may arise in further chapters.

Test & Test Case

A Test refers to a piece of code written to verify that another piece of code (usually a function or a class) works as expected. The term "test" in GoogleTest is the same as the term "test case" from International Software Testing Qualifications Board (ISTQB) materials and various textbooks on software quality.

To create a simple test in GoogleTest, use the TEST() macro. It defines and names a test function. This macro accepts name of the test suite and name of the test as parameter values. Note that it does not return any value.

Syntax

Syntax of the TEST() macro is given below −

TEST(TestSuiteName, TestName) {
  ... test body ...
}

Both test suite name and the test name must be valid C++ identifiers, and they should not contain any underscores (_).

Example

Following code shows an basic example of test in GoogleTest −

#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 
TEST(CheckDivison, ForPositiveInput) {
    EXPECT_EQ(12, divideNum(24, 2));
}

// main() function to run the test
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/test2/build
    Start 1: CheckDivison.ForPositiveInput
1/1 Test #1: CheckDivison.ForPositiveInput .......   Passed    0.02 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.2 sec

TestSuite & Test Case

From the initial days, GoogleTest was using the term "TestCase" for a group of related tests. However, ISTQB materials and various software quality textbooks use the term "test suite" for the same.

Therefore, recent versions of the GoogleTest started replacing the term "TestCase" with the "test suite". Now, the preferred API is TestSuite.

Test Suite GoogleTest

To create a test suite, write multiple tests with the single test suite name. But, each test should have a unique name as shown below −

// test 1
TEST(TestSuiteName, FirstTestName) {
  ... test body ...
}
// test 2
TEST(TestSuiteName, SecondTestName) {
  ... test body ...
}

Example

In the following code, we are creating a test suite to test a division function in GoogleTest −

#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, divide(4, -2));
    EXPECT_EQ(-2, divide(-4, 2));
    EXPECT_EQ(2, divide(-4, -2));
}

// test 3
TEST(CheckDivison, ForZeroDenominator) {
    EXPECT_THROW(divide(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 this code, it will display the following output −

Test project D:/gTest/test2/build
    Start 1: CheckDivison.ForPositiveInput
1/3 Test #1: CheckDivison.ForPositiveInput .....   Passed    0.03 sec
    Start 2: CheckDivison.ForNegativeInput
2/3 Test #2: CheckDivison.ForNegativeInput .....   Passed    0.01 sec
    Start 3: CheckDivison.ForZeroDenominator
3/3 Test #3: CheckDivison.ForZeroDenominator ...   Passed    0.02 sec

100% tests passed, 0 tests failed out of 3

Total Test time (real) =   0.10 sec
Advertisements