收藏 分销(赏)

使用gtest的测试学习.doc

上传人:仙人****88 文档编号:11234983 上传时间:2025-07-09 格式:DOC 页数:10 大小:353KB 下载积分:10 金币
下载 相关 举报
使用gtest的测试学习.doc_第1页
第1页 / 共10页
使用gtest的测试学习.doc_第2页
第2页 / 共10页


点击查看更多>>
资源描述
实验1 代码如下: // gtest_demo.cpp : Defines the entry point for the console application. #include "stdafx.h" #include <gtest/gtest.h> //added codes int Foo(int a, int b) { if (a == 0 || b == 0) { throw "don't do that"; //这是什么东东??还是个关键字“throw” } int c = a % b; 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)); //可以在上一句的逗号前加上“<<”像用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" #include <gtest.h> //继承testing::Environment类 class FooEnvironment : public testing::Environment { public: virtual void SetUp() { std::cout << "Foo FooEnvironment SetUP" << std::endl; } virtual void TearDown() { std::cout << "Foo FooEnvironment TearDown" << std::endl; } }; int _tmain(int argc, _TCHAR* argv[]) { //当然,这样还不够,我们还需要告诉gtest添加这个全局事件,我们需要在main函数中//通过testing::AddGlobalTestEnvironment方法将事件挂进来,也就是说,我们可以写很//多个这样的类,然后将他们的事件都挂上去。 testing::AddGlobalTestEnvironment(new FooEnvironment); testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下图: 实验3事件机制之TestSuite的 #include "stdafx.h" #include <gtest/gtest.h> //继承testing::Test,然后实现两个静态方法 class TestFixture : public testing::Test { public: static void SetUpTestCase()//为所有test 初始化 { s_i = 2; } static 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::AddGlobalTestEnvironment(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 <gtest/gtest.h> //一个test fixture,继承testing::Test类 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(派生至testing::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 : Defines the entry point for the console application. // #include "stdafx.h" #include <gtest.h> // Returns true iff n is a prime number. bool IsPrime(int n) { // Trivial case 1: small numbers if (n <= 1) return false; // Trivial case 2: even numbers if (n % 2 == 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/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<T>,其中T就是你需要参数化的参数类型 class IsPrimeParamTest : public::testing::TestWithParam<int> { }; //注意:是TEST_P TEST_P(IsPrimeParamTest, HandleTrueReturn) { int n = GetParam(); EXPECT_TRUE(IsPrime(n)); } //INSTANTIATE_TEST_CASE_P这宏来告诉gtest你要测试的参数范围 INSTANTIATE_TEST_CASE_P( TrueReturn, IsPrimeParamTest, testing::Values(3, 5, 11, 23, 17) ); int _tmain(int argc, _TCHAR* argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下: 实验6 死亡测试 #include "stdafx.h" #include <gtest.h> void Foo() { int *pInt = 0; *pInt = 42 ; } TEST(FooDeathTest, Demo) //使用DeathTest后缀,FooDeathTest是测试用例名 { EXPECT_DEATH(Foo(), ""); //注意,这里应该是Foo(),而不是Foo,为什么呢?原因很简单,这是一个测试用例,函数参数什么的必须确定下来。 } int _tmain(int argc, _TCHAR* argv[]) { testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } 结果如下: 如果把测试代码改成 void Foo() { } 结果如下: 实验7 命令行参数 #include "stdafx.h" #include <gtest.h> #include <tchar.h> int DieInDebugElse12(int* sideeffect) { if (sideeffect) *sideeffect = 12; std::cout<<"i am a fool.11111"<<std::endl; #ifdef NDEBUG std::cout<<"i am a fool.222222"<<std::endl; //GTEST_LOG_(FATAL, "debug 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 没见过啊 #ifdef 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"); return 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); //添加下面一行代码 testing::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
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 包罗万象 > 大杂烩

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2025 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服