1、实验1
代码如下:
// gtest_demo.cpp : Defines the entry point for the console application.
#include "stdafx.h"
#include
2、 if (c == 0) return b; return Foo(b, c); } TEST(FooTest, HandleNoneZeroInput) //这个宏的两个参数是用来干嘛的呢???仅仅是为测试取个名字?? { //EXPECT_EQ(2, Foo(4, 10)); //here it is right EXPECT_EQ(3, Foo(4, 10)); //here it is wrong,结果如截图,原来这里的第一个参数是只Foo函数的返回值。 ASSERT_EQ(2, Foo(0, 10)); //可以在上
3、一句的逗号前加上“<<”像用count一样输出你认为重要的信息。 } int _tmain(int argc, _TCHAR* argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下图: 实验2 事件机制之全局的 // gtest_global.cpp : Defines the entry point for the console application. //这里没有任何的测试用例!! #include "stdafx.h" #inc
4、lude
5、Down" << std::endl; } }; int _tmain(int argc, _TCHAR* argv[]) { //当然,这样还不够,我们还需要告诉gtest添加这个全局事件,我们需要在main函数中//通过testing::AddGlobalTestEnvironment方法将事件挂进来,也就是说,我们可以写很//多个这样的类,然后将他们的事件都挂上去。 testing::AddGlobalTestEnvironment(new FooEnvironment); testing::InitGoogleTest(&argc,
6、 argv);
return RUN_ALL_TESTS();
}
结果如下图:
实验3事件机制之TestSuite的
#include "stdafx.h"
#include
7、 void TearDownTestCase()//最后清除 { } protected: static int s_i; }; int TestFixture::s_i = 0; //TEST_F这个宏,第一个参数必须是我们上面类的名字,代表一个TestSuite。 TEST_F(TestFixture, test1) { EXPECT_EQ(2, s_i);//访问共享数据。 } int _tmain(int argc, _TCHAR* argv[]) { //testing::AddGlobalTestEnviro
8、nment(new TestFixture);
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
结果如下:
实验4 事件机制之TestCase的
// gtest_testcase.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
9、 class TestFixture : public testing::Test { public: virtual void SetUp() //用于初始化,在每个test 调用前调用。 { m_test = 1; } virtual void TearDown() //用于清除垃圾,在每个test 调用后调用. { } protected: int m_test; }; TEST_F(TestFixture, Test1)//这是一个test,第一个参数必须是TestFixture(派生至testi
10、ng::Test 的类)。 { EXPECT_EQ(1, m_test); //访问了TestFixture 类的m_test. } int _tmain(int argc, _TCHAR* argv[]) { //testing::AddGlobalTestEnvironment(new TestFixture); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下: 实验5 参数化 // gtest_param222.cpp
11、 : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
12、 == 0) return n == 2; // Now, we have that n is odd and n >= 3. // Try to divide n by every odd number i, starting from 3 for (int i = 3; ; i += 2) { // We only have to try i up to the squre root of n if (i > n/i) break; // Now, we have i <= n
13、/i < n.
// If n is divisible by i, n is not prime.
if (n % i == 0) return false;
}
// n has no integer factor in the range (1, n), and thus is prime.
return true;
}
//必须添加一个类,继承testing::TestWithParam
14、ing::TestWithParam
15、
);
int _tmain(int argc, _TCHAR* argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
结果如下:
实验6 死亡测试
#include "stdafx.h"
#include
16、emo) //使用DeathTest后缀,FooDeathTest是测试用例名 { EXPECT_DEATH(Foo(), ""); //注意,这里应该是Foo(),而不是Foo,为什么呢?原因很简单,这是一个测试用例,函数参数什么的必须确定下来。 } int _tmain(int argc, _TCHAR* argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下: 如果把测试代码改成 void Foo() { } 结果如下
17、
实验7 命令行参数
#include "stdafx.h"
#include 18、ebug death inside DieInDebugElse12()");//what is this ?添加这一句,测试程序会显示崩溃。不会继续向下执行了
#endif // NDEBUG
return 12;
}
TEST(TestCase, TestDieOr12WorksInDgbAndOpt)
{
int sideeffect = 0;
EXPECT_DEBUG_DEATH(DieInDebugElse12(&sideeffect), "death");//EXPECT_DEBUG_DEATH 没见过啊
#i 19、fdef NDEBUG// 如果是Release版本
EXPECT_EQ(4, sideeffect);
#else
EXPECT_EQ(0, sideeffect);
#endif
}
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
testing::FLAGS_gtest_death_test_style = "fast";
int flag = RUN_ALL_TESTS();
system("pause");
retu 20、rn flag;
}
1. 系统环境变量
2. 命令行参数
在xp虚拟控制台下进入到测试用例可执行文件夹下面,比如进入到Debug目录下,运行gtest_cmd_param.exe --gtest_output=xml(注意:这里的gtest_output前面有两个短杠‘- ’)
可在该目录下出现文件:test_detail.xml
3. 代码中指定的FLAG
关注添加的那一句:
int main(int argc, char** argv)
{
testing::InitGoogleTest(&argc, argv);
//添加下面一行代码
te 21、sting::GTEST_FLAG(output) = "xml";//这一句设置输出,代码中指定FLAG方式,这里是"output"而不是"-gtest_output"
testing::FLAGS_gtest_death_test_style = "fast";
int flag = RUN_ALL_TESTS();
system("pause");
return flag;
}
同时在D:\Backup\我的文档\Visual Studio 2008\Projects\gtest_cmd_param\gtest_cmd_param文件夹下面(我的电脑系统配置,个人配置不同,该文件夹路径也许不同),出现了一个文件: test_detail.xml






