You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Here are two Google Test examples, one using namespaces and the other using objects, along with explanations:
1. Test Example Using Namespaces:
#include"gtest/gtest.h"// Namespace for grouping related functionsnamespaceMathUtils {
intsquare(int x) {
return x * x;
}
}
// Test suite for functions in the MathUtils namespaceTEST(MathUtilsTest, SquareTest) {
EXPECT_EQ(MathUtils::square(5), 25);
EXPECT_EQ(MathUtils::square(-3), 9);
EXPECT_EQ(MathUtils::square(0), 0);
}
Namespace: The MathUtils namespace is used to organize the square function, making it clear it's part of a set of math-related utilities.
Test Suite: The MathUtilsTest suite specifically tests functions within the MathUtils namespace.
Accessing Functions: Functions within a namespace are accessed using the scope resolution operator (::).
2. Test Example Using Objects:
#include"gtest/gtest.h"// Class representing a rectangleclassRectangle {
public:Rectangle(int width, int height) : width_(width), height_(height) {}
intArea() const {
return width_ * height_;
}
private:int width_;
int height_;
};
// Test suite for the Rectangle classTEST(RectangleTest, AreaTest) {
Rectangle rect1(4, 5);
EXPECT_EQ(rect1.Area(), 20);
Rectangle rect2(2, 10);
EXPECT_EQ(rect2.Area(), 20);
}
Class: The Rectangle class defines a data type for rectangles with an Area method.
Test Suite: The RectangleTest suite focuses on testing the behavior of Rectangle objects.
Object Instantiation: The tests create Rectangle objects and call their Area method to verify the results.
Key Points:
Organization: Both namespaces and classes provide ways to organize your code logically.
Testing Focus: Namespaces are often used to group related functions, while classes typically encapsulate data and behavior, leading to different testing focuses.
Syntax: Notice the differences in how functions and methods are accessed in tests.
Running the Tests:
You'd compile and run these examples in a similar way as shown previously:
documentationImprovements or additions to documentation
1 participant
Heading
Bold
Italic
Quote
Code
Link
Numbered list
Unordered list
Task list
Attach files
Mention
Reference
Menu
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Here are two Google Test examples, one using namespaces and the other using objects, along with explanations:
1. Test Example Using Namespaces:
MathUtilsnamespace is used to organize thesquarefunction, making it clear it's part of a set of math-related utilities.MathUtilsTestsuite specifically tests functions within theMathUtilsnamespace.::).2. Test Example Using Objects:
Rectangleclass defines a data type for rectangles with anAreamethod.RectangleTestsuite focuses on testing the behavior ofRectangleobjects.Rectangleobjects and call theirAreamethod to verify the results.Key Points:
Running the Tests:
You'd compile and run these examples in a similar way as shown previously:
Beta Was this translation helpful? Give feedback.
All reactions