gmock

编写优美的GTest测试案例

 ̄綄美尐妖づ 提交于 2020-08-19 05:33:08
使用gtest也有很长一段时间了,这期间也积累了一些经验,所以分享一下。GTest为我们提供了便捷的测试框架,让我们只需要关注案例本身。如何在GTest框架下写出优美的测试案例,我觉得必须要做到: 案例的层次结构一定要清晰 案例的检查点一定要明确 案例失败时一定要能精确的定位问题 案例执行结果一定要稳定 案例执行的时间一定不能太长 案例一定不能对测试环境造成破坏 案例一定独立,不能与其他案例有先后关系的依赖 案例的命名一定清晰,容易理解 案例的可维护性也是非常重要,如果做到上面的8点,自然也就做到了可维护性。下面来分享一下我对于上面8点的经验: 1. 案例的层次结构一定要清晰 所谓层次结构,至少要让人一眼就能分辨出被测代码和测试代码。简单的说,就是知道你在测什么。由于是进行接口测试,我已经习惯了如下的案例层次: DataDefine 我会将测试案例所需要的数据,以及数据之间的联系全部在预先定义好。测试数据与案例逻辑的分离,有利于维护和扩展测试案例。同时,GTest先天就支持测试数据参数化,为测试数据的分离提供了进一步的便捷。什么是测试数据参数化?就是你可以预先定义好一批各种各样的数据,而你只需要编写一个测试案例的逻辑代码,gtest会将定义好的数据逐个套入测试案例中进行执行。具体的做法请见: 玩转Google开源C++单元测试框架Google Test系列(gtest)之四 -

How to use gmock MOCK_METHOD for overloaded operators?

耗尽温柔 提交于 2020-07-20 07:26:22
问题 I am new to googlemock (and StackOverflow). I got a problem when using MOCK_METHODn in googlemock and I believe this function is widely used. Here is what I did. I have an abstract class Foo with virtual overloaded operator[] : class Foo{ public: virtual ~Foo(){}; virtual int operator [] (int index) = 0; } and I want to use googlemock to get a MockFoo : class MockFoo: public Foo{ public: MOCK_METHOD1(operator[], int(int index)); //The compiler indicates this line is incorrect } However, this

gmock/google-mock issues warning and fails the test with mocking exceptions

强颜欢笑 提交于 2020-06-17 09:45:52
问题 I have coded a demo mock using google mock. The issue is that it is failing and not properly mocking. I cannot understand the issue here. Code: test/mock_turtle_test.cc #include "mock_turtle.h" #include "../painter.h" #include <gtest/gtest.h> using ::testing::_; using ::testing::AtLeast; using ::testing::Return; ACTION(MyException) { throw(error_k()); } TEST(PainterTest, CanDrawSomething) { Painter painter; EXPECT_CALL(*(painter.getTurtle()), PenDown()) .Times(AtLeast(1)) .WillOnce

google mock - can I call EXPECT_CALL multiple times on same mock object?

对着背影说爱祢 提交于 2020-05-20 09:33:41
问题 If I call EXPECT_CALL twice on the same mock object in the same TEST_F . . . what happens? Are the expectations appended to the mock object or does the second call erase the effects of the first call? I found The After Clause which appears to imply that multiple calls to same mock + EXPECT_CALL are allowed. 回答1: Yes, you can call EXPECT_CALL on the same mock object multiple times. As long as you assure that all EXPECT_CALL were called before the mocked methods were actually used. Otherwise

How to use gmock to mock up a std::function?

北慕城南 提交于 2020-05-16 03:25:07
问题 The constructor of my class is A( ... std::function<bool(const std::string&, const std::string&)> aCallBack, ... ); I want to use EXPECT_CALL to test it. This callback is from another class B. I created a Mock like class BMock : public B { MOCK_METHOD2( aCallBack, bool(const std::string&, const std::string&) ); } Then I tried B *b = new B(); std::function<bool(const std::string&, const std::string&)> func = std::bind(&B::aCallBack, b, std::PlaceHolders::_1, std::PlaceHolders::_2); It still

google mock - how to say “function must be called ONCE with a certain parameter but ok to be called many times with different parameters”?

心不动则不痛 提交于 2020-05-13 05:57:25
问题 I need to detect that a given function has been called exactly ONCE with a certain set of arguments. EXPECT_CALL(Mock_Obj, func("abc")).Times(1) but it's ok for that function to be called with different arguments any number of times. How do I express that? 回答1: In Google Mock, later expectations override earlier ones (more details in the docs), so you can write this: EXPECT_CALL(Mock_Obj, func(_)).Times(AnyNumber()); EXPECT_CALL(Mock_Obj, func("abc")).Times(1); 回答2: Do like VladLosev said.

google mock - how to say “function must be called ONCE with a certain parameter but ok to be called many times with different parameters”?

若如初见. 提交于 2020-05-13 05:57:15
问题 I need to detect that a given function has been called exactly ONCE with a certain set of arguments. EXPECT_CALL(Mock_Obj, func("abc")).Times(1) but it's ok for that function to be called with different arguments any number of times. How do I express that? 回答1: In Google Mock, later expectations override earlier ones (more details in the docs), so you can write this: EXPECT_CALL(Mock_Obj, func(_)).Times(AnyNumber()); EXPECT_CALL(Mock_Obj, func("abc")).Times(1); 回答2: Do like VladLosev said.

google mock - how to say “function must be called ONCE with a certain parameter but ok to be called many times with different parameters”?

旧时模样 提交于 2020-05-13 05:57:07
问题 I need to detect that a given function has been called exactly ONCE with a certain set of arguments. EXPECT_CALL(Mock_Obj, func("abc")).Times(1) but it's ok for that function to be called with different arguments any number of times. How do I express that? 回答1: In Google Mock, later expectations override earlier ones (more details in the docs), so you can write this: EXPECT_CALL(Mock_Obj, func(_)).Times(AnyNumber()); EXPECT_CALL(Mock_Obj, func("abc")).Times(1); 回答2: Do like VladLosev said.

How to mock methods return object with deleted copy-ctor?

耗尽温柔 提交于 2020-05-12 12:09:06
问题 If an interface has a function to create an object with deleted copy-ctor, how to mock this function? Gmock seems to use the object's copy constructor internally. E.g. // The object with deleted copy-ctor and copy-assignment class TTest { public: TTest() = delete; TTest(const TTest&) = delete; TTest& operator=(const TTest&) = delete; TTest(TTest&&) = default; TTest& operator=(TTest&&) = default; explicit TTest(int) { } }; // My interface to mock class MyInterface { public: virtual

gmock - how to mock function with noexcept specifier

谁都会走 提交于 2020-04-18 06:53:26
问题 I need to mock the following function: virtual void fun() noexcept = 0; Is it possible using gmock ? Gmock has the following macro: #define GMOCK_METHOD0_(tn, constness, ct, Method, ...) but there is a comment: // INTERNAL IMPLEMENTATION - DON'T USE IN USER CODE!!! Moreover I don't know how to use that macro (what the parameters tn and ct means) ? Edit The following mock: GMOCK_METHOD0_(, noexcept, ,fun, void()); compiles with gmock 1.7.0 but when I compile it using gmock 1.8.1 I get the