#include "stdafx.h" #include "foo.h" #include <gtest/gtest.h> class IsPrimeParamTest : public::testing::TestWithParam<int> { }; // 不使用参数化测试,就需要像这样写五次 TEST(IsPrimeTest, HandleTrueReturn) { EXPECT_TRUE(IsPrime(3)); EXPECT_TRUE(IsPrime(5)); EXPECT_TRUE(IsPrime(11)); EXPECT_TRUE(IsPrime(23)); EXPECT_TRUE(IsPrime(17)); } // 使用参数化测试,只需要: TEST_P(IsPrimeParamTest, HandleTrueReturn) { int n = GetParam(); EXPECT_TRUE(IsPrime(n)); } // 定义参数 INSTANTIATE_TEST_CASE_P(TrueReturn, IsPrimeParamTest, testing::Values(3, 5, 11, 23, 17)); // ----------------------- // 更复杂一点的参数结构 struct NumberPair { NumberPair(int _a, int _b) { a = _a; b = _b; } int a; int b; }; class FooParamTest : public ::testing::TestWithParam<NumberPair> { }; TEST_P(FooParamTest, HandleThreeReturn) { FooCalc foo; NumberPair pair = GetParam(); EXPECT_EQ(3, foo.Calc(pair.a, pair.b)); } INSTANTIATE_TEST_CASE_P(ThreeReturn, FooParamTest, testing::Values(NumberPair(12, 15), NumberPair(18, 21))); |