Prompt Detail:
Sure, here's an example GoogleTest code with comments that covers the basic features:
cpp#include <gtest/gtest.h>
// Fixture class for setting up and tearing down test cases
class MyFixture : public ::testing::Test {
protected:
void SetUp() override {
// Code to run before each test case
}
void TearDown() override {
// Code to run after each test case
}
int my_variable_ = 42; // Fixture member variable
};
// Tests that a condition is true
TEST(TestSuiteName, TestName) {
EXPECT_TRUE(true);
}
// Tests that a condition is false
TEST(TestSuiteName, TestName2) {
EXPECT_FALSE(false);
}
// Tests that two values are equal
TEST_F(MyFixture, TestName3) {
EXPECT_EQ(my_variable_, 42);
}
// Tests that two values are not equal
TEST_F(MyFixture, TestName4) {
EXPECT_NE(my_variable_, 43);
}
// Tests that a value is greater than another value
TEST_F(MyFixture, TestName5) {
EXPECT_GT(my_variable_, 41);
}
// Tests that a value is less than another value
TEST_F(MyFixture, TestName6) {
EXPECT_LT(my_variable_, 43);
}
// Tests that a value is greater than or equal to another value
TEST_F(MyFixture, TestName7) {
EXPECT_GE(my_variable_, 42);
}
// Tests that a value is less than or equal to another value
TEST_F(MyFixture, TestName8) {
EXPECT_LE(my_variable_, 42);
}
// Tests that a function throws an exception
TEST(TestSuiteName, TestName9) {
EXPECT_THROW(throw std::runtime_error("Error!"), std::runtime_error);
}
// Tests that a function does not throw an exception
TEST(TestSuiteName, TestName10) {
EXPECT_NO_THROW(42);
}
// Tests that a function takes less than a specified amount of time to run
TEST(TestSuiteName, TestName11) {
EXPECT_TRUE(true); // Placeholder for time-based test
}
// Main function to run all tests
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
This code includes the following features:
- Fixture class for setting up and tearing down test cases (
MyFixture
) - Assertion macros for testing true/false conditions (
EXPECT_TRUE
,EXPECT_FALSE
) - Assertion macros for testing equality and inequality (
EXPECT_EQ
,EXPECT_NE
) - Assertion macros for testing greater than/less than relationships (
EXPECT_GT
,EXPECT_LT
,EXPECT_GE
,EXPECT_LE
) - Assertion macros for testing exceptions (
EXPECT_THROW
,EXPECT_NO_THROW
) - Placeholder for time-based test (
EXPECT_TRUE(true)
) - Main function to run all tests
Of course, this is just a basic example, and there are many more features and options available in GoogleTest.
Add a comment