googletest--Death Test和Exception Test
阿新 • • 發佈:2019-01-12
Death Test驗證某個狀態會使程序以某個錯誤碼和錯誤訊息離開
#include <gtest\gtest.h> #include "MyStack.h" // Death Test: 驗證某個狀態會使程序以某個錯誤碼和錯誤訊息離開
void function1() { std::cerr << "Bad thing happened"; std::exit(1); // exit(0) 不算death } void function2() { std::cerr << "Bad thing happend again."; std::exit(22); } TEST(tutorial_DeathTest, simpletest) { // 轉換:testcase名字需要以DeathTest結尾 // 因為DeathTests往往需要在其他test之前允許 int x = 90; // death的原因: // 1. 程序以非0值離開 // 2. 程序被一個訊號殺死 EXPECT_DEATH(function1(), "Bad thing happened"); //正則表示式 EXPECT_EXIT(function2(), ::testing::ExitedWithCode(22), ""); // "" 表示不關心錯誤訊息 //EXPECT_EXIT(function2(), ::testing::KilledBySignal(SIGKILL), ".*"); // Windows上沒有
EXPECT_THROW(st.pop(), std:out_of_range); // 預期st.pop()拋一個std:out_of_range異常.
EXPECT_ANY_THROW(st.pop()); // 預期st.pop()拋一個任意型別的異常
EXPECT_NO_THROW(st.pop()); // 預期st.pop()不跑異常
switch () {
case -1: FAIL() << "Size can not be negative"; // 報告測試失敗
}