GTest使用

来源:互联网 发布:ant windows 64位下载 编辑:程序博客网 时间:2024/05/16 05:15

GTest

一、环境

1.    下载

http://googletest.googlecode.com/files/gtest-1.3.0.zip

 

2.    编译

进入msvc目录,编译生成库文件

 

3.    搭建环境

头文件

C/C++/General/Additional include Directories/

库文件

Linker/Input/Additional Dependencies/

 

4.    测试

TEST(funTest, HandleNoneZeroInput){}

main函数

testing::InitGoogleTest(&argc, argv);

RUN_ALL_TESTS();

 

二、断言

1.ASSERT_*检查失败退出当前函数

EXPECT_*检查失败继续往下执行

 

添加调试信息

EXPECT_EQ()<<“xxx”;

 

2.布尔值检查

ASSERT_TRUE(condition)/ASSERT_FALSE(condition)

EXPECT_TRUE(condition)/EXPECT_FALSE(condition)

 

3.数值型检查

ASSERT_EQ(expected,actual)/EXPECT_EQ(expected, actual)       expected == actual

ASSERT_NE(val1, val2)/EXPECT_NE(val1,val2)                   val1!= val2

ASSERT_LT(val1, val2)/EXPECT_LT(val1,val2)                     val1< val2

ASSERT_LE(val1, val2)/EXPECT_LE(val1,val2)                     val1<= val2

ASSERT_GT(val1, val2)/EXPECT_GT(val1,val2)                   val1> val2

ASSERT_GE(val1, val2)/EXPECT_GE(val1,val2)                   val1>= val2

 

4.字符转检查

ASSERT_STREQ(expected_str,actual_str)/EXPECT_STREQ(expected_str, actual_str)    //have same content

ASSERT_STRNE(str1, str2)/EXPECT_STRNE(str1,str2)                          //havedifferent content

 

5.显示返回成功或失败

SUCCEES()

FAIL()

ADD_FAILURE()

 

6.异常检查

ASSERT_THROW(statement,exception_type)/EXPECT_THROW(statement, exception_type)       //throw an exception of the given type

ASSERT_ANY_THROW(statement)/EXPECT_ANT_THROW(statement)                       //throw an exception ofany type

 

7.Predicate Assert

ASSERT_PRED1(pred1, val1)/EXPECT_PRED1(pred1,val1)                //pred1(val1)returntrue

ASSERT_PRED2(pred2, val1, val2)/EXPECT_PRED2(pred2,val1, val2)         //pred2(val1,val2)return true

 

8.浮点型检查

ASSERT_FLOAT_EQ(expected,actual)/EXPECT_FLOAT_EQ(expected, actual)   //floatalmost equal

ASSERT_DOUBLE_EQ(expected,actual)/EXPECT_DOUBLE_EQ(expected, actual)   //doublealmost equal

对两个相近的数比较

ASSERT_NEAR(val1, val2,abs_error)/EXPECT_NEAR(val1, val2, abs_error)

 

9.Windows HRESULT assertions

ASSERT_HRESULT_SUCCESSED(expression)/EXPECT_HRESULT_SUCCESSED(expression)       //expression is a success HRESULT

ASSERT_HRESULT_FAILED(expression)/EXPECT_HRESULT_FAILED(expression)              //expression is a failure HRESULT

 

三、事件机制

1.全局事件:所有案例执行前后

class FooEnvironment : publictesting::Environment

 {

public:

    virtual void SetUp()

     {

        std::cout << "Foo FooEnvironment SetUP" <<std::endl;

     }

    virtual void TearDown()

     {

        std::cout << "Foo FooEnvironment TearDown" <<std::endl;

     }

 };

挂载(main函数中)

testing::AddGlobalTestEnvironment(newFooEnvironment);

 

2.TestSuite级别的:在某一批案例中第一个案例前,最后一个案例执行后

class FooTest : public testing::Test {

  protected:

  static void SetUpTestCase() {

    shared_resource_ = new ...;

   }

  static void TearDownTestCase() {

    delete shared_resource_;

    shared_resource_ = NULL;

   }

   //Some expensive resource shared by all tests.

 static T* shared_resource_;

 };

测试案例宏TEST_F,第一个参数必须是上面定义的类名字,代表一个TestSuite

TEST_F(FooTest, Test1)

{

     //you can refer to shared_resource here

}

TEST_F(FooTest, Test2)

{

     //you can refer to shared_resource here

}

 

3.TestCase级别的:每个TestCase前后

class FooCalcTest:public testing::Test

{

protected:

       virtualvoid SetUp()

       {

              m_foo.Init();

       }

       virtualvoid TearDown()

       {

              m_foo.Finalize();

       }

 

       FooCalcm_foo;

};

 

TEST_F(FooCalcTest, HandleNoneZeroInput)

{

       EXPECT_EQ(4,m_foo.Calc(12, 16));

}

 

TEST_F(FooCalcTest, HandleNoneZeroInput_Error)

{

       EXPECT_EQ(5,m_foo.Calc(12, 16));

}

 

四、参数化

1.告诉gtest参数类型

class ParamTest :public::testing::TestWithParam<int>

{

};

2.告诉gtest拿到参数具体做些什么测试

TEST_P(ParamTest, aLotTest)

{

       intn = GetParam();

       intm = GetParam();

       EXPECT_TRUE(m== n);

}

3.告诉gtest要测试的参数范围

INSTANTIATE_TEST_CASE_P(EqualParamTest,ParamTest, testing::Values(1, 10, 100, 1000, 10000));//参数生成器

第一个参数:测试案例的前缀,任意取

第二个参数:测试案例名称,需和之前定义的参数化的类的名称相同

第三个参数:

Range(begin, end[, step])               //范围在begin-end之间,步长step,不包括end

Values(v1, v2, v3...);                        //v1v2。。

ValuesIn(container) and ValuesIn(begin, end)      //从一个C类型的数组或是STL容器,或是迭代器中取值

Bool()                                      //falsetrue

Combine(g1, g2, ... gN)                          //

 

五、死亡测试

ASSERT_DEATH(statement,regex)/EXPECT_DEATH(statement, regex)                       //crasheswith the given error

ASSERT_EXIT(statement, predicate,regex)/EXPECT_EXIT(statement, predicate, regex)      //exitwith the given error

 

1.DEATH

void Foo()

 {

    int *pInt = 0;

    *pInt = 42 ;

 }

 

 TEST(FooDeathTest, Demo)

 {

    EXPECT_DEATH(Foo(), "");

 }

TEST的第一个参数,即testcase_name使用DeathTest后缀

 

2.EXIT

TEST(ExitDeathTest, Demo)

{

   EXPECT_EXIT(_exit(1), testing::ExitedWithCode(1), "");

}

 

3.运行方式

testing::FLAGS_gtest_death_test_style ="fast"/"threadsafe";

 

六、运行参数

设置输出xml

testing::GTEST_FLAG(output) ="xml:";

测试案例输出

--gtest_print_time

--gtest_output=xml:

--gtest_catch_exceptions

 

0 0
原创粉丝点击