GoogleTest - Assertions



The assertions in GoogleTest are macros that are similar to function calls. They are used to verify the behavior of your code in various ways. It can check Boolean conditions, compare values, verify string and floating-point values.

Here, the term macro refers to a pre-processor directive. It defines a piece of code that you can reuse multiple times throughout your tests.

If an assertion fails, a failure message will be printed along with the assertion's source file and line number where it failed. It is also possible to give a custom failure message which will be appended to failure message.

Types of Assertions

There are two types of assertion in GoogleTest, which are as follows −

  • EXPECT_*
  • ASSERT_*

Between both assertions, EXPECT_* is preferred as it allow to report more than one failure.

EXPECT_* Assertion

The EXPECT_* is a non-fatal assertion, which means that even if they fail, the test continues to run. In other words, it produces non-fatal failures. Due to this feature, you can use it to test multiple conditions without blocking the code.

Additionally, you are also allowed to provide custom failure messages to be appended to assertions using the << operator.

Common EXPECT_* Assertions

Some of the commonly used EXPECT_* assertions are −

  • EXPECT_TRUE(condition) − To test if the condition is TRUE.
  • EXPECT_EQ(val1, val2) − To perform equality test.
  • EXPECT_LT(val1, val2) − Tests if val1 is less than val2.
  • EXPECT_GT(val1, val2) − Tests if val1 is greater than val2.
  • EXPECT_THROW(statement, exception_type) − Expects a specific exception to be thrown.
  • EXPECT_STREQ(str1, str2) − Tests whether two given strings are equal.

Example

The following example demonstrates the use of EXPECT_* assertion.

#include <gtest/gtest.h>

// to test basic assertions
TEST(Test1, ExpectAssertions) {
  // expect two strings to be equal
  EXPECT_STREQ("DDaaCC", "DDaaCC");
  // expect equality
  EXPECT_EQ(12, 2 * 6);
}

On running, following output will be displayed −

Test project D:/gTest/test4/build
    Start 1: Test1.ExpectAssertions
1/1 Test #1: Test1.ExpectAssertions ...........   Passed    0.02 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.08 sec

ASSERT_* Assertion

Unlike EXPECT_*, the ASSERT_* assertions are fatal. If a failure occurs, it will immediately abort the current test function which will also prevents any further code from executing within that test suite.

You should use ASSERT_* if it doesn't make sense to continue or proceeding with the test could lead to misleading results.

Common ASSERT_* Assertions

Some of the commonly used ASSERT_* assertions are −

  • ASSERT_TRUE(condition) − Verifies if the condition is TRUE.
  • ASSERT_EQ(val1, val2) − Asserts that two values are equal.
  • ASSERT_LT(val1, val2) − Checks if the first value is less than the second.
  • ASSERT_GT(val1, val2) − Tests if the first value is greater than the second or not.
  • ASSERT_THROW(statement, exception_type) − Expects that a specific exception is thrown by the specified statement.

Example

Let's see a practical example of ASSERT_* assertion.

#include <gtest/gtest.h>

// to test basic assertions
TEST(Test1, AssertAssertions) {
  // assert that the condition is true
  ASSERT_TRUE(strcmp("DDaaCC", "DDaaCC") == 0);
  // assert first value is less than second 
  ASSERT_LT(2 * 6, 13);
}

On running this code, it will produce the following result −

Test project D:/gTest/test4/build
    Start 1: Test1.AssertAssertions
1/1 Test #1: Test1.AssertAssertions ...........   Passed    0.03 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.07 sec
Advertisements