什么是gtest
gtest是一個(gè)跨平臺(tái)的(Liunx、Mac OS X、Windows、Cygwin、Windows CE and Symbian)C++單元測(cè)試框架,由google公司發(fā)布。gtest是為在不同平臺(tái)上為編寫(xiě)C++測(cè)試而生成的。它提供了豐富的斷言、致命和非致命判斷、參數(shù)化、”死亡測(cè)試”等等。
官網(wǎng):GoogleTest
它分為好幾種測(cè)試工具。依次介紹:
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的自動(dòng)測(cè)試運(yùn)行程序和圖形用戶界面,具有Windows和Linux平臺(tái)的強(qiáng)大功能。
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是運(yùn)行測(cè)試程序的測(cè)試運(yùn)行程序,允許您通過(guò)進(jìn)度條跟蹤其進(jìn)度,并顯示測(cè)試失敗的列表。單擊其中一個(gè)顯示故障文本。谷歌測(cè)試用戶界面是用C#語(yǔ)言編寫(xiě)的。
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測(cè)試的事件偵聽(tīng)器,它實(shí)現(xiàn)了測(cè)試結(jié)果輸出的tap協(xié)議。如果您的測(cè)試人員理解TAP協(xié)議,您可能會(huì)發(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是一個(gè)測(cè)試運(yùn)行程序,它并行運(yùn)行可執(zhí)行程序中的測(cè)試,以提供顯著的加速。
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是一個(gè)允許在樹(shù)視圖中查看Google測(cè)試并運(yùn)行/調(diào)試測(cè)試的vs代碼擴(kuò)展。
如何使用
Exercise a particular program path with specific input values and verify the results。
使用特定的輸入值運(yùn)行特定的程序路徑并驗(yàn)證結(jié)果。
聽(tīng)起來(lái)比較繞口,其實(shí)就是一個(gè)叫做測(cè)試單元的概念。
先來(lái)解釋下test case
:
A set of preconditions, inputs, actions (where applicable), expected results and postconditions, developed based on test conditions.
基于測(cè)試條件開(kāi)發(fā)的一組先決條件、輸入、動(dòng)作(如適用)、預(yù)期結(jié)果和后置條件。
在gtest中的使用就是一個(gè)函數(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()宏定義和命名測(cè)試函數(shù),這些是不返回值的普通C++函數(shù)。
- In this function, along with any valid C++ statements you want to include, use the various googletest assertions to check values.
在這個(gè)函數(shù)中,連同任何要包含的有效C++語(yǔ)句,使用各種googletest assertions 來(lái)檢查值。
- 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.
測(cè)試結(jié)果由斷言確定;如果測(cè)試中的任何斷言失敗(致命或非致命),或者如果測(cè)試崩潰,則整個(gè)測(cè)試都失敗。否則,它會(huì)成功。
斷言(assertions)
gtest的使用離不開(kāi)斷言。什么是斷言?
Google Test斷言是類似于函數(shù)調(diào)用的宏。您可以通過(guò)對(duì)其行為進(jìn)行斷言來(lái)測(cè)試類或函數(shù)。當(dāng)斷言失敗時(shí),Google Test會(huì)打印斷言的源文件和行號(hào)位置以及失敗消息。
gtest中斷言的宏可以分為兩類:一類是ASSERT宏,另一類就是EXPECT宏了。
1 ASSERT_系列:如果當(dāng)前點(diǎn)檢測(cè)失敗則退出當(dāng)前函數(shù)
2 EXPECT_系列:如果當(dāng)前點(diǎn)檢測(cè)失敗則繼續(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ā)生故障時(shí),Google測(cè)試同時(shí)打印val1和val2。
而且值參數(shù)通過(guò)斷言的比較運(yùn)算符必須可以比較,否則會(huì)出現(xiàn)編譯錯(cuò)誤。
字符串
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”表示忽略大小寫(xiě)。
而且,NULL指針和空字符串被認(rèn)為是不同的。
異常檢查
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 |
測(cè)試用例
偽碼:
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谝粋€(gè)參數(shù)是測(cè)試用例的名稱,第二個(gè)參數(shù)是測(cè)試用例中的測(cè)試名稱。
兩個(gè)名稱必須是有效的C++標(biāo)識(shí)符,并且它們不應(yīng)該包含下劃線。
測(cè)試的全名由它的包含測(cè)試用例和它的單個(gè)名稱組成。來(lái)自不同測(cè)試用例的測(cè)試可以具有相同的單個(gè)名稱。
例子:
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按測(cè)試用例對(duì)測(cè)試結(jié)果進(jìn)行分組,因此邏輯上相關(guān)的測(cè)試應(yīng)該在同一個(gè)測(cè)試用例中;換句話說(shuō),它們的TEST()的第一個(gè)參數(shù)應(yīng)該相同。
在上面的例子中,我們有兩個(gè)測(cè)試,OneAddZeroInput和addSomeInput,它們屬于同一個(gè)測(cè)試用例addsumTest。
今天就描述到這里。明天繼續(xù)。O(∩_∩)O哈哈~