[英]Google Test - Using "SetUpTestSuite" doesn't seem to work
我正在嘗試編寫一個測試套件來執行測試套件級別的“設置”操作。
我試圖首先編寫一個簡單的程序來嘗試讓它工作,但我沒有任何運氣來調用“SetUpTestSuite”方法。
#include <gtest/gtest.h>
#include <iostream>
class MyTest : public ::testing::Test
{
protected:
static void SetUpTestSuite() {
std::cerr << "TestSuiteSetup" << std::endl;
}
static void TearDownTestSuite() {
}
};
TEST_F(MyTest, Case1) {
std::cerr << "TESTING" << std::endl;
}
int main(int argc, char* argv[])
{
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
當我運行這個時,我得到:
[==========] Running 1 tests from 1 test cases.
[----------] Global test environment set-up.
[----------] 1 test from MyTest
[ RUN ] MyTest.Case1
TESTING
[ OK ] MyTest.Case1 (0 ms)
[----------] 1 test from MyTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 tests from 1 test cases ran. (0 ms total)
[ PASSED ] 1 tests.
出於某種原因,從未調用SetUpTestSuite()
。
我一直在閱讀 Google 測試文檔的同一套件部分中的測試之間共享資源,但我無法弄清楚我做錯了什么。
有什么我想念的嗎?
注意:我使用的是 gtest v1.6.0 - 它是我公司 Red Hat RPM 存儲庫中唯一可用的軟件包。
文檔似乎是錯誤的。 這些方法應該被稱為SetUpTestCase()
和TearDownTestCase()
。 至少在Google Test 1.8.0中。
此更改似乎尚未發布。 這些文檔似乎是Master分支的最新版本,而不是已發布的版本。
如果您的被測代碼拋出異常,gtest 框架將捕獲它們並仍然使用TestDownTestSuite()
關閉。 但是,如果您在測試用例中啟動了一些線程並且它們拋出了,那么沒有什么可以捕獲它們並且您的進程可能會調用terminate()
並且該進程將立即停止並且不會被拆除。 確保在工作線程中捕獲異常,或使用std::async
創建std::future
s,它將自動捕獲異常並在原始線程的上下文中重新拋出它們(當您調用future.get()
),它將被gtest抓住了。
這發生在我身上,我開始使用不受支持的TEST_TIMEOUT_BEGIN()
和TEST_TIMEOUT_FAIL_END()
宏黑客( http://antonlipov.blogspot.com/2015/08/how-to-timeout-tests-in-gtest.html ),然后是異常開始發生。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.