CT 2025.06 supports GTest (Google Test) style test macros through the Code-Based test feature.
This page provides the types of macros available for Code-Based test, along with their descriptions and examples.
The test code editor provides auto-completion feature. Press Ctrl
+ Space
to view the list of available macros.
Test Definition Macro
These macros define the structure and execution unit of a test function.
Macro Form | Explanation | Example |
---|---|---|
CTEST(test_suite_name, test_case_name) |
Defines a test case under the test suite. | CTEST(MySuite, MyTest) { ... } |
CTEST_F(test_suite_name, test_case_name) |
Defines a test case using a custom fixture with the same name as the test suite. | CTEST_F(MyFixture, InitTest) { ... } |
CTEST_P(test_suite_name, test_case_name) |
Declares a parameterized test case. Arguments must be registered with INSTANTIATE_CTEST_SUITE_P .Within the test, arguments are loaded in order via GetParam(). |
CTEST_P(ParamSuite, CheckParam) { ... } |
INSTANTIATE_CTEST_SUITE_P(prefix, test_suite_name, parameters) |
Registers parameter values for a CTEST_P test suite declared earlier. |
INSTANTIATE_CTEST_SUITE_P(Default, ParamSuite, ::ctest::Values(1, 2)); |
Example :
// 1. CTEST: Basic test case. CTEST(Math, Addition) { CTEST_ASSERT_EQ(5, Add(2, 3)); } // 2. CTEST_F: Test using a common fixture. class CalculatorFixture : public ::ctest::Fixture { public: void SetUpTest() override { calculator.reset(); initial_value = 100; } void TearDownTest() override { } protected: Calculator calculator; int initial_value; }; CTEST_F(CalculatorFixture, WithInitialValue) { calculator.setValue(initial_value); CTEST_ASSERT_EQ(105, calculator.add(5)); } // 3. CTEST_P: Parameterized test. class EvenNumberTest : public ::ctest::TestWithParam<int> { public: void SetUpTest() override { } void TearDownTest() override { } }; CTEST_P(EvenNumberTest, CheckIfEven) { int value = GetParam(); CTEST_EXPECT(value % 2 == 0); } INSTANTIATE_CTEST_SUITE_P(EvenNumbers, EvenNumberTest, ::ctest::Values(0, 2, 4, 6, 8));
Basic condition check macro
Checks whether a single condition is true or false.
Macro Form | Explanation | Example |
---|---|---|
CTEST_EXPECT(condition) |
The macro passes the test when the condition is true. if it fails, it continues execution. | CTEST_EXPECT(x > 0); |
CTEST_ASSERT(condition) |
The macro passes the test when the condition is true. if it fails, it immediately aborts the test. | CTEST_ASSERT(ptr != nullptr); |
Value comparison macro
Checks the result of comparing integers or other general values.
Macro Form | Explanation | Example |
---|---|---|
CTEST_EXPECT_EQ(a, b) |
The macro passes the test when the two values are equal. if it fails, it continues execution. | CTEST_EXPECT_EQ(5, Add(2,3)); |
CTEST_ASSERT_EQ(a, b) |
The macro passes the test when the two values are equal. if it fails, it immediately aborts the test. | CTEST_ASSERT_EQ(42, GetValue()); |
CTEST_EXPECT_NE(a, b) |
The macro passes the test when the two values are not equal. if it fails, it continues execution. | CTEST_EXPECT_NE(status, -1); |
CTEST_ASSERT_NE(a, b) |
The macro passes the test when the two values are not equal. if it fails, it immediately aborts the test. | CTEST_ASSERT_NE(pointer, nullptr); |
CTEST_EXPECT_LT(a, b) |
The macro passes the test when a is less than b. if it fails, it continues execution. | CTEST_EXPECT_LT(index, array_size); |
CTEST_ASSERT_LT(a, b) |
The macro passes the test when a is less than b. if it fails, it immediately aborts the test. | CTEST_ASSERT_LT(position, buffer.size()); |
CTEST_EXPECT_LE(a, b) |
The macro passes the test when a is less than or equal to b. if it fails, it continues execution. | CTEST_EXPECT_LE(current_size, max_size); |
CTEST_ASSERT_LE(a, b) |
The macro passes the test when a is less than or equal to b. if it fails, it immediately aborts the test. | CTEST_ASSERT_LE(used_memory, max_memory); |
CTEST_EXPECT_GT(a, b) |
The macro passes the test when a is greater than b. if it fails, it continues execution. | CTEST_EXPECT_GT(score, 70); |
CTEST_ASSERT_GT(a, b) |
The macro passes the test when a is greater than b. if it fails, it immediately aborts the test. | CTEST_ASSERT_GT(new_count, old_count); |
CTEST_EXPECT_GE(a, b) |
The macro passes the test when a is greater than or equal to b. if it fails, it continues execution. | CTEST_EXPECT_GE(result, minimum); |
CTEST_ASSERT_GE(a, b) |
The macro passes the test when a is greater than or equal to b. if it fails, it immediately aborts the test. | CTEST_ASSERT_GE(available_space, required_space); |
String comparison macro
Checks whether strings are equal, with options for case-sensitive or case-insensitive comparison.
Macro Form | Explanation | Example |
---|---|---|
CTEST_EXPECT_STR_EQ(a, b) |
The macro passes the test when the two strings are exactly equal. if it fails, it continues execution. | CTEST_EXPECT_STR_EQ(result, "OK"); |
CTEST_ASSERT_STR_EQ(a, b) |
The macro passes the test when the two strings are exactly equal. if it fails, it immediately aborts the test. | CTEST_ASSERT_STR_EQ(output.c_str(), "PASS"); |
CTEST_EXPECT_STR_NE(a, b) |
The macro passes the test when the two strings are not equal. if it fails, it continues execution. | CTEST_EXPECT_STR_NE(input, "fail"); |
CTEST_ASSERT_STR_NE(a, b) |
The macro passes the test when the two strings are not equal. if it fails, it immediately aborts the test. | CTEST_ASSERT_STR_NE(msg.c_str(), "ERROR"); |
CTEST_EXPECT_STR_IGNORE_CASE_EQ(a, b) |
The macro passes the test when the two strings are equal, ignoring case. if it fails, it continues execution. | CTEST_EXPECT_STR_IGNORE_CASE_EQ(cmd, "start"); |
CTEST_ASSERT_STR_IGNORE_CASE_EQ(a, b) |
The macro passes the test when the two strings are equal, ignoring case. if it fails, it immediately aborts the test. | CTEST_ASSERT_STR_IGNORE_CASE_EQ(user_input, "YES"); |
CTEST_EXPECT_STR_IGNORE_CASE_NE(a, b) |
The macro passes the test when the two strings are not equal, ignoring case. if it fails, it continues execution. | CTEST_EXPECT_STR_IGNORE_CASE_NE(command, "exit"); |
CTEST_ASSERT_STR_IGNORE_CASE_NE(a, b) |
The macro passes the test when the two strings are not equal, ignoring case. if it fails, it immediately aborts the test. | CTEST_ASSERT_STR_IGNORE_CASE_NE(response, "abort"); |
Floating-point comparison macro
Performs an approximate comparison of floating-point values. Values are compared within a specified tolerance range due to the inherent difficulty of exact comparison.
Macro Form | Explanation | Example |
---|---|---|
CTEST_EXPECT_FLOAT_EQ(a, b) |
The macro passes the test when the difference between two float values is less than or equal to 1e-5f. if it fails, it continues execution. | CTEST_EXPECT_FLOAT_EQ(calc, 3.14159f); |
CTEST_ASSERT_FLOAT_EQ(a, b) |
The macro passes the test when the difference between two float values is less than or equal to 1e-5f. if it fails, it immediately aborts the test. | CTEST_ASSERT_FLOAT_EQ(gravity, 9.81f); |
CTEST_EXPECT_DOUBLE_EQ(a, b) |
The macro passes the test when the difference between two double values is less than or equal to 1e-12. if it fails, it continues execution. | CTEST_EXPECT_DOUBLE_EQ(pi, 3.14159265359); |
CTEST_ASSERT_DOUBLE_EQ(a, b) |
The macro passes the test when the difference between two double values is less than or equal to 1e-12. if it fails, it immediately aborts the test. | CTEST_ASSERT_DOUBLE_EQ(result, expected); |
CTEST_EXPECT_NEAR(a, b, tol) |
The macro passes the test when the difference between two values is less than or equal to the specified tolerance (tol). if it fails, it continues execution. | CTEST_EXPECT_NEAR(measurement, standard, 0.001); |
CTEST_ASSERT_NEAR(a, b, tol) |
The macro passes the test when the difference between two values is less than or equal to the specified tolerance (tol). if it fails, it immediately aborts the test. | CTEST_ASSERT_NEAR(output, 1.0, 0.0001); |
Exception check macro
Checks whether an exception is thrown during code execution.
Macro Form | Explanation | Example |
---|---|---|
CTEST_EXPECT_THROW(stmt, ex_type) |
The macro passes the test when the specified statement throws an exception of the expected type. if it fails, it continues execution. | CTEST_EXPECT_THROW(func(), std::runtime_error); |
CTEST_ASSERT_THROW(stmt, ex_type) |
The macro passes the test when the specified statement throws an exception of the expected type. if it fails, it immediately aborts the test. | CTEST_ASSERT_THROW(open(), std::logic_error); |
CTEST_EXPECT_NO_THROW(stmt) |
The macro passes the test when the specified statement does not throw any exception. if it fails, it continues execution. | CTEST_EXPECT_NO_THROW(safe_call()); |
CTEST_ASSERT_NO_THROW(stmt) |
The macro passes the test when the specified statement does not throw any exception. if it fails, it immediately aborts the test. | CTEST_ASSERT_NO_THROW(do_work()); |
CTEST_EXPECT_ANY_THROW(stmt) |
The macro passes the test when the specified statement throws any type of exception. if it fails, it continues execution. | CTEST_EXPECT_ANY_THROW(dangerous()); |
CTEST_ASSERT_ANY_THROW(stmt) |
The macro passes the test when the specified statement throws any type of exception. if it fails, it immediately aborts the test. | CTEST_ASSERT_ANY_THROW(unstable()); |
Log macro
Outputs log messages during test execution for debugging or informational purposes.
Macro Form | Explanation | Example |
---|---|---|
CTEST_LOG(msg) |
Print user log. | CTEST_LOG("Test started"); |
CTEST_LOG_FORMAT(fmt, ...) |
Print user logs in the specified format. | CTEST_LOG_FORMAT("count=%d, result=%s", count, "PASS"); |
Mock-related macros
Use mock objects to control dependencies and verify the results of function calls.
Macro Form | Explanation | Example |
---|---|---|
CMOCK_METHOD(returnType, methodName, args, specifiers) |
Defines a mock method named methodName in a mock object. For non-void methods, SetReturn_methodName can set return values. | CMOCK_METHOD(int, Add, (int a, int b), const override); |
Example :
class MockFoo : public Foo { CMOCK_METHOD(int, Add, (int a, int b), const override); }; CTEST(MockTest, AddCalled) { MockFoo mock; mock.SetReturn_Add(42); CTEST_ASSERT_EQ(42, mock.Add(2, 3)); }
Need more help with this?
Don’t hesitate to contact us here.