提供两种判断的宏 EXPECT_XXX()(推荐使用) ASSERT_XXX()(出错直接 crash)
分组测试
#include <gtest/gtest.h>
int Factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int add(int a, int b) { return a + b; }
/**
* 下面三个 TEST 都是属于同一个 test suite,即 FactorialTest
* (期待值,真实值)
*/
// 正数为一组
TEST(FactorialTest, Positive) {
// TEST 函数中类似普通函数,可定义变量
EXPECT_EQ(1, Factorial(1));
EXPECT_EQ(2, Factorial(2));
EXPECT_EQ(6, Factorial(3));
EXPECT_EQ(40320, Factorial(8));
}
// 负数为一组
TEST(FactorialTest, Negative) {
EXPECT_EQ(1, Factorial(-5));
EXPECT_EQ(1, Factorial(-1));
EXPECT_GT(Factorial(-10), 0);
}
/**
* 另一组测试
*/
TEST(AddTest, 01) {
EXPECT_EQ(3, add(1, 2));
EXPECT_LT(3, add(2, 2));
}
int main(int argc, char* argcv[]) {
testing::InitGoogleTest(&argc, argcv);
return RUN_ALL_TESTS();
}
SetUP TearDown
#include <gtest/gtest.h>
#include <iostream>
#include <queue>
class QueueTest : public testing::Test {
public:
std::queue<int> q;
virtual void SetUp() override {
std::cout << "before a case" << std::endl;
q.push(1);
q.push(2);
}
virtual void TearDown() override { std::cout << "after a case" << std::endl; }
static void SetUpTestCase() { std::cout << "before all case" << std::endl; }
static void TearDownTestCase() { std::cout << "after all case" << std::endl; }
};
TEST_F(QueueTest, TestSize) {
EXPECT_EQ(2, q.size());
q.pop();
EXPECT_EQ(1, q.size());
}
TEST_F(QueueTest, TestEmpty) {
EXPECT_FALSE(q.empty());
}
int main(int argc, char* argcv[]) {
testing::InitGoogleTest(&argc, argcv);
return RUN_ALL_TESTS();
}
标签:std,int,XXX,gtest,include,public From: https://www.cnblogs.com/zhh567/p/17044912.html