google test assert throw

Certainly! Here's an example:

#include <gtest/gtest.h>

int Divide(int a, int b) {
    if (b == 0) {
        throw std::invalid_argument("Division by zero");
    }
    return a / b;
}

TEST(DivideTest, HandlesZeroDivisor) {
    EXPECT_THROW(Divide(10, 0), std::invalid_argument);
}

TEST(DivideTest, HandlesNonZeroDivisor) {
    EXPECT_EQ(Divide(10, 2), 5);
    EXPECT_EQ(Divide(8, 4), 2);
}

Explanation: 1. #include <gtest/gtest.h> - includes the Google Test framework header file.

  1. Divide function defines division operation between two integers. It checks if the divisor is zero and throws an std::invalid_argument exception if true.

  2. TEST(DivideTest, HandlesZeroDivisor) - defines a test case named DivideTest with the specific test named HandlesZeroDivisor. This test checks whether the Divide function throws an exception when dividing by zero using EXPECT_THROW.

  3. EXPECT_THROW(Divide(10, 0), std::invalid_argument); - expects that the Divide function called with arguments (10, 0) will throw an std::invalid_argument exception.

  4. TEST(DivideTest, HandlesNonZeroDivisor) - defines another test case under DivideTest named HandlesNonZeroDivisor. This test checks the expected results when dividing by a non-zero number.

  5. EXPECT_EQ(Divide(10, 2), 5); - expects that the result of dividing 10 by 2 using the Divide function is equal to 5.

  6. EXPECT_EQ(Divide(8, 4), 2); - expects that the result of dividing 8 by 4 using the Divide function is equal to 2.