什么是gtest
gtest是一個跨平臺的(Liunx均驶、Mac OS X、Windows婴噩、Cygwin骆撇、Windows CE and Symbian)C++單元測試框架瞒御,由google公司發(fā)布。gtest是為在不同平臺上為編寫C++測試而生成的神郊。它提供了豐富的斷言肴裙、致命和非致命判斷、參數(shù)化屿岂、”死亡測試”等等践宴。
官網(wǎng):GoogleTest
它分為好幾種測試工具。依次介紹:
GTest Runner
GTest Runner is a Qt5 based automated test-runner and Graphical User Interface with powerful features for Windows and Linux platforms.
GTest Runner是基于qt5的自動測試運行程序和圖形用戶界面爷怀,具有Windows和Linux平臺的強大功能阻肩。
Google Test UI
Google Test UI is test runner that runs your test binary, allows you to track its progress via a progress bar, and displays a list of test failures. Clicking on one shows failure text. Google Test UI is written in C#.
Google Test UI是運行測試程序的測試運行程序,允許您通過進度條跟蹤其進度运授,并顯示測試失敗的列表烤惊。單擊其中一個顯示故障文本。谷歌測試用戶界面是用C#語言編寫的吁朦。
GTest TAP Listener
GTest TAP Listener is an event listener for Google Test that implements the TAP protocol for test result output. If your test runner understands TAP, you may find it useful.
gtest-tap-listener是Google測試的事件偵聽器柒室,它實現(xiàn)了測試結(jié)果輸出的tap協(xié)議。如果您的測試人員理解TAP協(xié)議逗宜,您可能會發(fā)現(xiàn)它很有用雄右。
gtest-parallel
gtest-parallel is a test runner that runs tests from your binary in parallel to provide significant speed-up.
gtest-parallel是一個測試運行程序,它并行運行可執(zhí)行程序中的測試纺讲,以提供顯著的加速擂仍。
oogleTest Adapter
GoogleTest Adapter is a VS Code extension allowing to view Google Tests in a tree view, and run/debug your tests.
GoogleTest Adapter是一個允許在樹視圖中查看Google測試并運行/調(diào)試測試的vs代碼擴展。
如何使用
Exercise a particular program path with specific input values and verify the results熬甚。
使用特定的輸入值運行特定的程序路徑并驗證結(jié)果逢渔。
聽起來比較繞口,其實就是一個叫做測試單元的概念乡括。
先來解釋下test case
:
A set of preconditions, inputs, actions (where applicable), expected results and postconditions, developed based on test conditions.
基于測試條件開發(fā)的一組先決條件肃廓、輸入智厌、動作(如適用)、預期結(jié)果和后置條件盲赊。
在gtest中的使用就是一個函數(shù):
TEST()
Simple Tests
To create a test:
- Use the
TEST()
macro to define and name a test function, These are ordinary C++ functions that don't return a value.
使用TEST()宏定義和命名測試函數(shù)铣鹏,這些是不返回值的普通C++函數(shù)。
- In this function, along with any valid C++ statements you want to include, use the various googletest assertions to check values.
在這個函數(shù)中角钩,連同任何要包含的有效C++語句吝沫,使用各種googletest assertions 來檢查值呻澜。
- The test's result is determined by the assertions; if any assertion in the test fails (either fatally or non-fatally), or if the test crashes, the entire test fails. Otherwise, it succeeds.
測試結(jié)果由斷言確定递礼;如果測試中的任何斷言失敗(致命或非致命)羹幸,或者如果測試崩潰脊髓,則整個測試都失敗。否則栅受,它會成功将硝。
斷言(assertions)
gtest的使用離不開斷言。什么是斷言屏镊?
Google Test斷言是類似于函數(shù)調(diào)用的宏依疼。您可以通過對其行為進行斷言來測試類或函數(shù)。當斷言失敗時而芥,Google Test會打印斷言的源文件和行號位置以及失敗消息律罢。
gtest中斷言的宏可以分為兩類:一類是ASSERT宏,另一類就是EXPECT宏了棍丐。
1 ASSERT_系列:如果當前點檢測失敗則退出當前函數(shù)
2 EXPECT_系列:如果當前點檢測失敗則繼續(xù)往下執(zhí)行
bool值檢查
ASSERT_ | EXPECT_ | Verifies |
---|---|---|
ASSERT_TRUE(condition); | EXPECT_TRUE(condition); | condition is true |
ASSERT_FALSE(condition); | EXPECT_FALSE(condition); | condition is false |
數(shù)值型
ASSERT_ | EXPECT_ | Verifies |
---|---|---|
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 |
在發(fā)生故障時误辑,Google測試同時打印val1和val2。
而且值參數(shù)通過斷言的比較運算符必須可以比較歌逢,否則會出現(xiàn)編譯錯誤巾钉。
字符串
ASSERT_ | Verifies |
---|---|
ASSERT_STREQ(expected_str,actual_str); | the two C strings have the same content |
ASSERT_STRNE(str1, str2); | the two C strings have different content |
ASSERT_STRCASEEQ(expected_str,actual_str); | the two C strings have the same content, ignoring case |
ASSERT_STRCASENE(str1, str2); | the two C strings have different content, ignoring case |
EXPECT_ | Verifies |
---|---|
EXPECT_STREQ(expected_str,actual_str); | the two C strings have the same content |
EXPECT_STRNE(str1, str2); | the two C strings have different content |
EXPECT_STRCASEEQ(expected_str,actual_str); | the two C strings have the same content, ignoring case |
EXPECT_STRCASENE(str1, str2); | the two C strings have different content, ignoring case |
注:斷言名中的“CASE”表示忽略大小寫。
而且秘案,NULL指針和空字符串被認為是不同的砰苍。
異常檢查
ASSERT_ | Verifies |
---|---|
ASSERT_THROW(statement, exception_type); | statement throws an exception of the given type |
ASSERT_ANY_THROW(statement); | statement throws an exception of any type |
ASSERT_NO_THROW(statement); | statement doesn't throw any exception |
EXPECT_ | Verifies |
---|---|
EXPECT_THROW(statement, exception_type); | statement throws an exception of the given type |
EXPECT_ANY_THROW(statement); | statement throws an exception of any type |
EXPECT_NO_THROW(statement); | statement doesn't throw any exception |
測試用例
偽碼:
TEST(TestSuiteName, TestName) {
... test body ...
}
TEST() arguments go from general to specific. The first argument is the name of the test case, and the second argument is the test's name within the test case. Both names must be valid C++ identifiers, and they should not contain underscore (_). A test's full name consists of its containing test case and its individual name. Tests from different test cases can have the same individual name.
TEST() 參數(shù)從常規(guī)變?yōu)樘囟ā5谝粋€參數(shù)是測試用例的名稱阱高,第二個參數(shù)是測試用例中的測試名稱赚导。
兩個名稱必須是有效的C++標識符,并且它們不應(yīng)該包含下劃線讨惩。
測試的全名由它的包含測試用例和它的單個名稱組成辟癌。來自不同測試用例的測試可以具有相同的單個名稱。
例子:
int add_sum(int a, int b)
{
return a + b;
}
TEST(addsumTest, OneAddZeroInput) {
EXPECT_EQ(add_sum(1,0), 1);
}
TEST(addsumTest, addSomeInput) {
EXPECT_EQ(add_sum(1, 0), 1);
EXPECT_EQ(add_sum(2, 0), 2);
EXPECT_EQ(add_sum(3, 3), 6);
EXPECT_EQ(add_sum(8, 1024), 40320);
}
放張截圖:O(∩_∩)O哈哈~
GoogleTest按測試用例對測試結(jié)果進行分組荐捻,因此邏輯上相關(guān)的測試應(yīng)該在同一個測試用例中黍少;換句話說寡夹,它們的TEST()的第一個參數(shù)應(yīng)該相同。
在上面的例子中厂置,我們有兩個測試菩掏,OneAddZeroInput和addSomeInput,它們屬于同一個測試用例addsumTest昵济。
今天就描述到這里智绸。明天繼續(xù)。O(∩_∩)O哈哈~