实验1
代码如下:
// : Defines the entry point for the console application.
#include ""
#include <gtest/>
//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 事件机制之全局的
// : Defines the entry point for the console application.
//这里没有任何的测试用例!!
#include ""
#include <>
//继承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::InitGo
使用gtest的测试学习 来自淘豆网m.daumloan.com转载请标明出处.