“死亡测试”用于测试程序是否会按照预期的方式崩溃。
1.执行顺序
编写死亡测试时,在测试中第一个参数以“DeathTest”为结尾,运行测试时会优先于其他测试用例
using namespace std;
void Foo(){
int *p=0;
*p = 42;
}
int Add(int a,int b){
return a+b;
}
TEST(TestAdd, demo1){
EXPECT_EQ(3,Add(1,2));
cout<<"demo1 run........"<<endl;
}
TEST(FooDeathTest, demo2){
EXPECT_DEATH(Foo(),"");
cout<<"DeathTest........"<<endl;
}
int main(int argc, char *argv[])
{
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
测试结果:
2.正则表达式
*_DEATH()语句中第二个变量可使用正则表达式匹配崩溃程序输出的异常信息
简单示例
A? matches 0 or 1 occurrences of A
void Foo(){
int a = 1;
int b = 0;
int c = a/b;
}
TEST(FooDeathTest, demo2){
EXPECT_DEATH(Foo(),"A?");
cout<<"DeathTest........"<<endl;
}
测试结果:
源码地址