If you are a completely stranger to mock object concept, wiki page is a good start! When and how to use mock object is as much a coding issue as a design choice. Things will get more tricky when the interdependence's among various components and third party APIs where the visibility is usually not ideal. You may also want to make sure the mock object can be easily replaced later stage by the real implementation. There is a general mix up that test framework is equivalent to mock objects, or offers mock up facilities. First thing is that is so not true although you might naturally decide to test your production code by creating some mock ups when those implementations from the other lazy boy do not turn up on time. In this sense, mock objects could be part of test harness definition. I have always believed that we should separate the mock object themselves from test framework as much as possible. Otherwise, you will be in the middle of confusions between test logic, mockup logic and production code fairy easily.
Here, I will only go through a few off-the-shelf mock object packages for different languages.
C++: Googlemock, Not surprisingly, I typed "GMock" in google when I started this blog. As I thought it is safely to assume that there is nothing those people do not do. After some internal tips, Goolgemock eventually joined my starred projects. Googlemock is quite tightly bound with Googletest which I talked a bit in previous blog. Although in the latest release, it seems to break this dependency. Googlemock is pretty much based on expectation model by setting up the expected call results and validating later in the logic code. Just as a comparison, Mockcpp is a typical recall&replay model implementation. the reason I am not a particular fan of Mockcpp is that it confuses itself what it is from start, a test framework or mock objects package, or both?
C#: Rhino mocks originates from EasyMock.Net which was a pure recall&replay mocking model. Rhino mocks integrates the RR model with expectation model which NMock heavily relies on. One plus for me is that Rhino mocks seems to be quite independent to any test framework while NMock is normally used together with NUnit test framework. There is a simple comparison for some of the popular mock up schemes in .Net.
C: It is not that straight forward to adopt this concept to traditional C world and embedded system development. The group in Atomic object has definitely made some marks here by introducing Unity (an embedded unit test framework for C) and CMock to the embedded world, and also nice Eclipse plugins to make people like me life easier. So Kudos to Atomic!
No comments:
Post a Comment