Junior — Middle
Have you created test files for modules or components in your development practice?
sobes.tech AI
Answer from AI
Yes, in C/C++ development practice, creating test files for modules or components is a common practice. It helps to:
- Verify the correctness of individual parts of the program.
- Ensure regression testing when changes are made.
- Simplify debugging and code maintenance.
Frameworks like Google Test (gtest) are often used for this purpose. An example of a simple test using gtest:
#include <gtest/gtest.h>
int add(int a, int b) {
return a + b;
}
TEST(AdditionTest, HandlesPositiveNumbers) {
EXPECT_EQ(add(2, 3), 5);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
This approach improves the quality and reliability of the code.