Writing a simple test
A simple test in Google Test can be written using the TEST
macro, which defines a test function. Within this function, you can use various assertions to verify the behavior of your code. Here’s a basic example:
#include <gtest/gtest.h> int add(int a, int b) { return a + b; } TEST(AdditionTest, HandlesPositiveNumbers) { EXPECT_EQ(5, add(2, 3)); }
In this example, EXPECT_EQ
is used to assert that the add
function returns the expected sum of two positive numbers. Google Test provides a variety of assertions such as EXPECT_GT
(greater than), EXPECT_TRUE
(Boolean true
), and many others for different testing scenarios.
The key difference between EXPECT_*
and ASSERT_*
assertions lies in their behavior upon failure. While EXPECT_*
assertions allow the test to continue running after a failure, ASSERT_*
assertions will halt the current test function immediately upon failure. Use EXPECT_*
when subsequent...