配置
-
下载gtest源码,解压后用VS打开
...\googletest-master\googletest\msvc\2010\gtest-md.sln
,打开后可以看到包含的4个项目
- 打开
生成 - 批生成(快捷键Alt+B+T)
,勾选所有64位的选项,点击生成
-
...\googletest-master\googletest\msvc\2010\gtest-md\x64-Debug
中将生成gtestd.lib
和gtest_maind.lib
-
...\googletest-master\googletest\msvc\2010\gtest-md\x64-Release
中将生成gtest.lib
和gtest_main.lib
- 头文件的库目录位于
...\googletest-master\googletest\include
,复制库目录和.lib文件到环境中,然后在VS中添加库目录和.lib文件即可使用
示例
#include <iostream>
#include <gtest/gtest.h>
int f(int n)
{
return n + 1;
}
TEST(testCase, case0)
{
EXPECT_EQ(1, f(0));
ASSERT_EQ(2, f(1));
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
}
error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MTd_StaticDebug”不匹配值“MDd_DynamicDebug”
- 将
配置属性 - C/C++ - 代码生成 - 运行库
由多线程调试 DLL (/MDd)
修改为多线程调试 (/MTd)
即可。原因是编译的是gtest.sln
而不是gtest-md.sln
,gtest.sln
的配置是多线程调试 (/MTd)
,而gtest-md.sln
的与VS的默认配置相同,是多线程调试 DLL (/MDd)
- 如果运行成功,将在控制台窗口中显示测试通过的结果
#include <iostream>
#include <gtest/gtest.h>
int f(int n)
{
return n + 1;
}
TEST(testCase, case0)
{
EXPECT_EQ(1, f(0));
EXPECT_EQ(2, f(1));
}
TEST(testCase, case1)
{
EXPECT_EQ(11, f(0));
EXPECT_EQ(22, f(1));
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
}
常用宏
ASSERT_TRUE(condition);
ASSERT_FALSE(condition);
ASSERT_EQ(expected, actual);
ASSERT_NE(expected, actual);
ASSERT_GT(expected, actual);
ASSERT_LT(expected, actual);
ASSERT_GE(expected, actual);
ASSERT_LE(expected, actual);
ASSERT_STREQ(expected, actual);
ASSERT_STRNE(expected, actual);
ASSERT_FLOAT_EQ(expected, actual);
ASSERT_DOUBLE_EQ(expected, actual);
ASSERT_NEAR(val1, val2, abs_error);
ASSERT_DEATH(statement, regex);
EXPECT_* // 同上
- ASSERT和EXPECT的区别是,若ASSERT失败,则不会执行下一条语句,而EXPECT会
值参数化测试
- 多次使用仅参数不同的同一判断条件,可以使用值参数化来简化代码
#include <iostream>
#include <gtest/gtest.h>
bool isOdd(int n)
{
return n % 2;
}
class ParamTest : public ::testing::TestWithParam<int> {};
TEST_P(ParamTest, case0)
{
EXPECT_TRUE(isOdd(GetParam())); // 多次EXPECT_TRUE
}
INSTANTIATE_TEST_CASE_P(TestSuite, ParamTest, ::testing::Values(1, 3, 5, 7, 9));
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
}
全局事件
#include <iostream>
#include <gtest/gtest.h>
class GlobalEnv : public ::testing::Environment
{
public:
virtual void SetUp() override
{
std::cout << "GlobalEnv::SetUp()" << std::endl;
}
virtual void TearDown() override
{
std::cout << "GlobalEnv::TearDown()" << std::endl;
}
};
TEST(TestCase, case0)
{
EXPECT_EQ(1, 2);
}
TEST(TestCase, case1)
{
EXPECT_EQ(1, 3);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
::testing::Environment* env = new GlobalEnv();
::testing::AddGlobalTestEnvironment(env);
RUN_ALL_TESTS();
std::cin.get();
}
fixture
#include <iostream>
#include <gtest/gtest.h>#include <iostream>
#include <gtest/gtest.h>
class TestSuite : public ::testing::Test
{
public:
static void SetUpTestCase()
{
std::cout << "SetUpTestCase()" << std::endl;
}
static void TearDownTestCase()
{
std::cout << "TearDownTestCase()" << std::endl;
}
virtual void SetUp() override
{
std::cout << "TestSuite::SetUp()" << std::endl;
}
virtual void TearDown() override
{
std::cout << "TestSuite::TearDown()" << std::endl;
}
};
TEST_F(TestSuite, case0)
{
EXPECT_EQ(1, 2);
}
TEST_F(TestSuite, case1)
{
EXPECT_EQ(1, 3);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
}
class TestSuite : public ::testing::Test
{
public:
static void SetUpTestCase()
{
std::cout << "SetUpTestCase()" << std::endl;
}
static void TearDownTestCase()
{
std::cout << "TearDownTestCase()" << std::endl;
}
virtual void SetUp() override
{ // 这里还可以构造对象,比如auto p = new Widget;
std::cout << "TestSuite::SetUp()" << std::endl;
}
virtual void TearDown() override
{ // 这里还可以析构对象,比如delete p;
std::cout << "TestSuite::TearDown()" << std::endl;
}
};
TEST_F(TestSuite, case0)
{
EXPECT_EQ(1, 2);
}
TEST_F(TestSuite, case1)
{
EXPECT_EQ(1, 3);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
RUN_ALL_TESTS();
std::cin.get();
}
结合使用
#include <iostream>
#include <gtest/gtest.h>
class GlobalEnv : public ::testing::Environment
{
public:
virtual void SetUp() override
{
std::cout << "GlobalEnv::SetUp()" << std::endl;
}
virtual void TearDown() override
{
std::cout << "GlobalEnv::TearDown()" << std::endl;
}
};
class TestSuite : public ::testing::Test
{
public:
static void SetUpTestCase()
{
std::cout << "SetUpTestCase()" << std::endl;
}
static void TearDownTestCase()
{
std::cout << "TearDownTestCase()" << std::endl;
}
virtual void SetUp() override
{
std::cout << "TestSuite::SetUp()" << std::endl;
}
virtual void TearDown() override
{
std::cout << "TestSuite::TearDown()" << std::endl;
}
};
TEST(TestCase, case0)
{
EXPECT_EQ(1, 2);
}
TEST(TestCase, case1)
{
EXPECT_EQ(1, 3);
}
TEST_F(TestSuite, case0)
{
EXPECT_EQ(1, 2);
}
TEST_F(TestSuite, case1)
{
EXPECT_EQ(1, 3);
}
int main(int argc, char **argv)
{
::testing::InitGoogleTest(&argc, argv);
::testing::Environment* env = new GlobalEnv();
::testing::AddGlobalTestEnvironment(env);
RUN_ALL_TESTS();
std::cin.get();
}
网友评论