首页 > 其他分享 >单元测试框架 CMock gMock

单元测试框架 CMock gMock

时间:2022-11-23 19:36:35浏览次数:63  
标签:some gMock 单元测试 but CMock objects your mock

 

 

https://github.com/ThrowTheSwitch/CMock

CMock 

CMock is a mock and stub generator and runtime for unit testing C. It's been designed to work smoothly with Unity Test, another of the embedded-software testing tools developed by ThrowTheSwitch.org. CMock automagically parses your C headers and creates useful and usable mock interfaces for unit testing. Give it a try!

If you don't care to manage unit testing builds yourself, consider checking out Ceedling, a test-centered build manager for unit testing C code.

 

 

 

https://google.github.io/googletest/gmock_for_dummies.html

What Is gMock?

When you write a prototype or test, often it’s not feasible or wise to rely on real objects entirely. A mock object implements the same interface as a real object (so it can be used as one), but lets you specify at run time how it will be used and what it should do (which methods will be called? in which order? how many times? with what arguments? what will they return? etc).

It is easy to confuse the term fake objects with mock objects. Fakes and mocks actually mean very different things in the Test-Driven Development (TDD) community:

  • Fake objects have working implementations, but usually take some shortcut (perhaps to make the operations less expensive), which makes them not suitable for production. An in-memory file system would be an example of a fake.
  • Mocks are objects pre-programmed with expectations, which form a specification of the calls they are expected to receive.

If all this seems too abstract for you, don’t worry - the most important thing to remember is that a mock allows you to check the interaction between itself and code that uses it. The difference between fakes and mocks shall become much clearer once you start to use mocks.

gMock is a library (sometimes we also call it a “framework” to make it sound cool) for creating mock classes and using them. It does to C++ what jMock/EasyMock does to Java (well, more or less).

When using gMock,

  1. first, you use some simple macros to describe the interface you want to mock, and they will expand to the implementation of your mock class;
  2. next, you create some mock objects and specify its expectations and behavior using an intuitive syntax;
  3. then you exercise code that uses the mock objects. gMock will catch any violation to the expectations as soon as it arises.

Why gMock?

While mock objects help you remove unnecessary dependencies in tests and make them fast and reliable, using mocks manually in C++ is hard:

  • Someone has to implement the mocks. The job is usually tedious and error-prone. No wonder people go great distance to avoid it.
  • The quality of those manually written mocks is a bit, uh, unpredictable. You may see some really polished ones, but you may also see some that were hacked up in a hurry and have all sorts of ad hoc restrictions.
  • The knowledge you gained from using one mock doesn’t transfer to the next one.

In contrast, Java and Python programmers have some fine mock frameworks (jMock, EasyMock, etc), which automate the creation of mocks. As a result, mocking is a proven effective technique and widely adopted practice in those communities. Having the right tool absolutely makes the difference.

gMock was built to help C++ programmers. It was inspired by jMock and EasyMock, but designed with C++’s specifics in mind. It is your friend if any of the following problems is bothering you:

  • You are stuck with a sub-optimal design and wish you had done more prototyping before it was too late, but prototyping in C++ is by no means “rapid”.
  • Your tests are slow as they depend on too many libraries or use expensive resources (e.g. a database).
  • Your tests are brittle as some resources they use are unreliable (e.g. the network).
  • You want to test how your code handles a failure (e.g. a file checksum error), but it’s not easy to cause one.
  • You need to make sure that your module interacts with other modules in the right way, but it’s hard to observe the interaction; therefore you resort to observing the side effects at the end of the action, but it’s awkward at best.
  • You want to “mock out” your dependencies, except that they don’t have mock implementations yet; and, frankly, you aren’t thrilled by some of those hand-written mocks.

We encourage you to use gMock as

  • design tool, for it lets you experiment with your interface design early and often. More iterations lead to better designs!
  • testing tool to cut your tests’ outbound dependencies and probe the interaction between your module and its collaborators.

标签:some,gMock,单元测试,but,CMock,objects,your,mock
From: https://www.cnblogs.com/sinferwu/p/16919524.html

相关文章

  • 003. Mybatis介绍---03单元测试与JUnit4
    1.JUnit4使用方法  2.pom.xml<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.1......
  • 进大厂必须要会的单元测试
    本文将按照如下顺序给大家简单讲讲单元测试应该怎么写什么是单元测试单元测试又称模块测试,是针对软件设计的最小单位(模块)就行正确性的校验的测试,检查每个程序模块是否实......
  • 单元测试中常见错误
    单元测试中常见错误单元的常见错误一般出现在5个方面:代码的稳定、易读、规范、易维护、专业。因此,单元测试的关注的重点有5点:单元接口、局部数据结构、边界条件......
  • 单元测试:会变化的定义
    有一种东西,如果它太小,需要付出的努力就太大;如果它太大,就很难测试。没错!它是单元。但是什么才是一个好的单元定义呢?为什么它如此重要? 单元的定义对测试过程有很大的......
  • Junit单元测试
    单元测试单元测试叫做模块测试,属于白盒测试,就是开发者编写的一小段代码,用于检测某个功能是否正确。白盒测试是懂代码的开发做的,需要对项目代码的进行编写测试代码。黑......
  • 单元测试
    MockitoMockito1.为什么要使用mock2.Mockito中常用方法2.1Mock方法2.2对Mock出来的对象进行行为验证和结果断言2.3给Mock对象打桩2.4Mock静态方法3.Mocki......
  • 单元测试2
    packagecom.echo.mockito.service.Impl;importcom.echo.mockito.dao.SalesDao;importcom.echo.mockito.dao.UserDao;importcom.echo.mockito.entity.User;importcom.e......
  • 使用Mockito与Squaretest进行单元测试.
    项目开发过程中,不少公司都要求写单元测试的代码,可以提高代码的质量,并且可以减少出现BUG的概率。对于中小型公司来说,对单元测试不做硬性要求,不写最好。因为还是需要一......
  • 单元测试 request_mock模拟网络调用
    importunittestfromunittestimportmockfromrequests.exceptionsimportConnectionErrorimportrequests_mockimportrequestsclassMyBugzilla:def__......
  • Springboot单元测试Junit的坑及解决方案
    最近做springboot项目,写单元测试导入junit的时候,org.junit.jupiter.api.Test和org.junit.Test傻傻分不清,因为习惯了用junit4,所以导入的都是org.junit.Test,普通的测试是没......