問題描述
我看到該功能的文檔似乎非常重要,因為它位于 Google 測試概述功能中,并在:
https://github.com/google/googletest/blob/master/docs/advanced.md#death-tests
I saw the documentation of that feature is seem pretty major since it's in Google Test overview features and detailed in:
https://github.com/google/googletest/blob/master/docs/advanced.md#death-tests
它們看起來像標準的 assert()
但它們是 Google Test 的一部分,因此是一個 xUnit 測試框架.因此,我想知道使用這些死亡測試的真正用途或優勢是什么.
They look like standard assert()
but they're part of Google Test, so a xUnit testing framework. Therefore, I wonder what the real usage or advantage of using those death tests are.
推薦答案
斷言是為了確認一個函數如果在當前進程中執行會導致程序終止(詳細說明死亡測試是從一個允許測試繼續進行的子進程,盡管死亡).這很有用,因為某些代碼可以保證程序在失敗時終止/中止(例如,如果存在不可恢復的錯誤),并且單元測試應確認函數遵守其記錄的行為,而不管它可能是什么.
The assertion is there to confirm that a function would bring about program termination if it were executed in the current process (the details explains that the death test is invoked from a subprocess which allows the tests to continue despite the death). This is useful because some code may guarantee program termination / abortion on failure (e.g. if there was an irrecoverable error), and unit tests should confirm that a function adheres to its documented behavior, regardless of what that might be.
wiki 頁面上的描述確實最好地解釋了它:
The description on the wiki page really explains it best:
在許多應用程序中,如果不滿足某個條件,斷言可能會導致應用程序失敗.這些確保程序處于已知良好狀態的健全性檢查會在某些程序狀態損壞后盡早失敗.如果斷言檢查了錯誤的條件,則程序可能會以錯誤的狀態繼續運行,這可能導致內存損壞、安全漏洞或更糟.因此,測試此類斷言語句是否按預期工作至關重要.
In many applications, there are assertions that can cause application failure if a condition is not met. These sanity checks, which ensure that the program is in a known good state, are there to fail at the earliest possible time after some program state is corrupted. If the assertion checks the wrong condition, then the program may proceed in an erroneous state, which could lead to memory corruption, security holes, or worse. Hence it is vitally important to test that such assertion statements work as expected.
這篇關于什么是 Google 測試、死亡測試的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!