C++ 什么是 Google Test、Death Tests

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3698718/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:30:45  来源:igfitidea点击:

What are Google Test, Death Tests

c++testinggoogletest

提问by Wernight

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/googletest/docs/advanced.md#death-tests

我看到该功能的文档似乎非常重要,因为它位于 Google 测试概述功能中,并在以下位置进行了详细说明:https:
//github.com/google/googletest/blob/master/googletest/docs/advanced.md#death-tests

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 advantageof using those death testsare.

它们看起来很标准,assert()但它们是 Google Test 的一部分,因此是一个 xUnit 测试框架。因此,我想知道使用这些死亡测试的真正用途或优势是什么。

采纳答案by Michael Aaron Safyan

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.

断言是为了确认一个函数在当前进程中执行时会导致程序终止(详细说明死亡测试是从子进程调用的,该子进程允许测试继续进行,尽管死亡)。这很有用,因为某些代码可以保证程序在失败时终止/中止(例如,如果存在不可恢复的错误),并且单元测试应确认函数遵守其记录的行为,而不管它可能是什么。

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.

在许多应用程序中,如果不满足某个条件,断言可能会导致应用程序失败。这些确保程序处于已知良好状态的健全性检查会在某些程序状态损坏后尽早失败。如果断言检查了错误的条件,则程序可能会以错误的状态继续运行,这可能导致内存损坏、安全漏洞或更糟。因此,测试此类断言语句是否按预期工作非常重要。

回答by Jon Cage

I thought the introduction in your link explained it fairly well:

我认为您链接中的介绍很好地解释了它:

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.

Since these precondition checks cause the processes to die, we call such tests death tests. More generally, any test that checks that a program terminates in an expected fashion is also a death test.

在许多应用程序中,如果不满足某个条件,断言可能会导致应用程序失败。这些确保程序处于已知良好状态的健全性检查会在某些程序状态损坏后尽早失败。如果断言检查了错误的条件,则程序可能会以错误的状态继续运行,这可能导致内存损坏、安全漏洞或更糟。因此,测试此类断言语句是否按预期工作非常重要。

由于这些先决条件检查会导致进程终止,因此我们称此类测试为死亡测试。更一般地说,任何检查程序是否以预期方式终止的测试也是死亡测试。

What bit of that doesn't make sense?

哪一点没有意义?