C++ gtest 和 gmock 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13696376/
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-27 17:38:57  来源:igfitidea点击:

What is the difference between gtest and gmock?

c++googletestgooglemock

提问by Rasmi Ranjan Nayak

I'm trying to understand the purpose of google-mock, Google's C++ mocking framework.

我想了解的目的google-mock谷歌的C ++嘲讽框架

I have already worked with gtestearlier, but still I can't understand what gmockis. Why do we need it?

我之前已经使用gtest过,但我仍然无法理解是什么gmock。为什么我们需要它?

gtestis used for unit testing. What do we need gmockfor then, if gmockis required for unit testing?

gtest用于单元测试。我们需要什么gmock的话,如果gmock需要unit testing

回答by dans3itz

"Google Mock is not a testing framework itself. Instead, it needs a testing framework for writing tests. Google Mock works seamlessly with Google Test. It comes with a copy of Google Test bundled. Starting with version 1.1.0, you can also use it with any C++ testing framework of your choice. " - Google Mock, System Requirements

“Google Mock 本身并不是一个测试框架。相反,它需要一个用于编写测试的测试框架。Google Mock 与 Google Test 无缝协作。它附带了 Google Test 的副本。从 1.1.0 版开始,您还可以使用它与您选择的任何 C++ 测试框架。” - Google Mock,系统要求

Mock are like objects, defined in such a way to mimick the real-deal by supplying controlled behavior. For instance, to test a stock tick application, you'd create a fake stock data provider that created fake stock quotes to test your code with. Think of the word mock, literally means 'to mimic'.

模拟就像对象,定义为通过提供受控行为来模仿真实交易。例如,要测试股票报价应用程序,您需要创建一个虚假股票数据提供者,该提供者创建虚假股票报价来测试您的代码。想想模拟这个词,字面意思是“模仿”。

回答by Thinkeye

Software units do not live in green meadows. They very often need some counterparts to do the work. In real system, these counterparts belong to the system itself. In the unit tests they are replaced with mocks.

软件单位不生活在绿色草地上。他们经常需要一些同行来做这项工作。在实际系统中,这些对应物属于系统本身。在单元测试中,它们被替换为模拟。

Gtest is a framework for unit testing. Gmock is a framework imitating the rest of your system during unit tests.

Gtest 是一个单元测试框架。Gmock 是一个在单元测试期间模仿系统其余部分的框架。

回答by Stephen Bloch

Suppose you're writing a piece of code that needs to interact with an unpredictable, expensive, external system (e.g. a Web site, a large database, a physical sensor, etc.) Your code needs to keep working when the external system times out, or gives you error messages, or gives you inconsistent data. How can you test whether your code actually meets those requirements?

假设您正在编写一段代码,需要与不可预测的、昂贵的外部系统(例如网站、大型数据库、物理传感器等)进行交互。当外部系统超时时,您的代码需要继续工作,或者给你错误信息,或者给你不一致的数据。您如何测试您的代码是否真正满足这些要求?

Well, you could run it against an actual Web site/database/sensor/whatever, a whole bunch of times, and hope you luck into all the error conditions that your code is supposed to handle. Obviously a fairly expensive and unreliable testing strategy.

好吧,您可以针对实际的网站/数据库/传感器/任何东西运行它很多次,并希望您能遇到您的代码应该处理的所有错误情况。显然,这是一种相当昂贵且不可靠的测试策略。

So instead, you write something that satisfies the same interface as the Web site/database/sensor/whatever, but which you've programmed to produce certain "canned" responses (errors, timeouts, inconsistent data, etc.) Your tests will now run much faster (because they don't face the overhead of a realWeb site/database/sensor/whatever), and they're predictable. Unfortunately, it takes a lot of coding to write up a separate "mock" Web site/database/sensor/whatever for each scenario you need to test. The more work it is, the less likely you are to do it. Result: inadequately tested code.

因此,相反,您编写的内容满足与网站/数据库/传感器/任何内容相同的界面,但您已对其进行编程以生成某些“预设”响应(错误、超时、不一致的数据等)。您的测试现在将运行得更快(因为它们不会面临真正的网站/数据库/传感器/任何东西的开销),而且它们是可预测的。不幸的是,为您需要测试的每个场景编写一个单独的“模拟”网站/数据库/传感器/任何东西都需要大量的编码。工作越多,你做它的可能性就越小。结果:未充分测试的代码。

Gmock and its relatives automate a lot of this stuff, so you can specify the desired "canned" behavior in the middle of the test itself, at the cost of only a few lines of code. If tests are easy to write, you're likely to write more of them, and therefore more likely to discover bugs before shipping the code :-)

Gmock 和它的亲戚自动化了很多这样的东西,所以你可以在测试本身的中间指定所需的“罐头”行为,只需要几行代码。如果测试易于编写,您可能会编写更多的测试,因此更有可能在交付代码之前发现错误 :-)

BTW, this implies that you also need "dependency injection": your code needs to take in a parameter of the interface type, and you need to be able to pass in either a mock object (for unit-testing) or the "real" Web site/database/sensor/whatever (for real-world use).

顺便说一句,这意味着您还需要“依赖注入”:您的代码需要接受接口类型的参数,并且您需要能够传入模拟对象(用于单元测试)或“真实”网站/数据库/传感器/任何(用于实际使用)。

回答by VINOTH ENERGETIC

Let say you want to write something to file.

假设你想写一些东西到文件中。

You have to test like system memory is full or not.

您必须测试系统内存是否已满。

Will you make the system memory full to test this? No.

你会让系统内存满来测试这个吗?不。

Your friend Google mock will help you here.

你的朋友谷歌模拟会在这里帮助你。