473,802 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

a library of stock mock objects?

There is a (relatively) widely used technique in unit testing, called
mock objects. There is even a pMock library which provides a Mock class
for a Python environment.

Given the "duck typing" nature of the Python itself, it's pretty
trivial to build mocks without using any pre-built libraries. What is
less trivial and potentially more worthwhile is to have a library of
"stock" mock objects.

For instance, I found myself re-implementing ConnectionStub class again
and again for different projects. And there is no shortage of other
good candidates as well: socket, web request/response, thread objects,
etc. Someone on a Python mailing list asks whether anyone implemented a
mock filesystem interface, for example.

Having a library of such mock classes, realizing widely-used and
"heavy" interfaces may be a good idea.

The only real problem I can think of is whether it's possible to make
these mocks generic enough. Mocks often contain some
(application-specific) hard-coded rules and data to realize desired
behavior. For instance, socket mock may fail when user attempts to
write a "foobar" string - and unit test will use this to check
how code handle these kind of errors. These rules may be generalized
but it may lead to mock classes becoming unwieldy.

Even keeping this problem in mind, it's still a reasonable idea for a
library, but obviously I'm biased.

What do you think?

Jul 19 '05 #1
1 1788
> Given the "duck typing" nature of the Python itself, it's pretty
trivial to build mocks without using any pre-built libraries.
I've also found it trivial to build a custom mock object whenever the
need arises.
What is
less trivial and potentially more worthwhile is to have a library of
"stock" mock objects. . . . Having a library of such mock classes, realizing widely-used and
"heavy" interfaces may be a good idea.

The only real problem I can think of is whether it's possible to make
these mocks generic enough.
There's the rub.

In writing tests, we frequently want to simulate only a tiny subset of
behaviors and that subset varies dramatically depending on who is
writing the test and what is being tested. For the most part, there is
no "one size fits all".

Given the ease of writing minimal, custom mock objects, I think it is
not worthwhile to try to create a library of them.
Mocks often contain some
(application-specific) hard-coded rules and data to realize desired
behavior. For instance, socket mock may fail when user attempts to
write a "foobar" string - and unit test will use this to check
how code handle these kind of errors. These rules may be generalized
but it may lead to mock classes becoming unwieldy.
And also hard to learn, maintain, etc.

When it comes to writing unittests, there is a distinct advangtage from
writing your own, minimal mock objects. The writer knows exactly which
behaviors are being modeled. In constrast, a library of heavy-weight
mock objects promises to model many, but not all behaviors. Without a
thorough knowledge of the library's fidelity, there is an undue risk of
writing tests that look more thorough than they actually are.

Likewise, there arises a double maitainance issue of keeping the mock
object library in-sync with the real library being simulated.
Even keeping this problem in mind, it's still a reasonable idea for a
library, but obviously I'm biased.

What do you think?


I think it is like trying to market custom-cut, application specific
duck tape. The generic tear-it-and-tape-it solution is far easier and
more versatile than having a "library" of pre-cut tape.
Raymond Hettinger

Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1977
by: John J. Lee | last post by:
I had the bright idea a week or two ago to construct mock objects (for unit testing) "by example" by calling methods on a generic mock object. Say we have a class Bar, and we want to check that, when we use BarUser in a particular way, BarUser first calls method Bar.xyzzy with args "foo" and bar=2, then method Bar.do_nothing. # first, construct a mock object by example: m = Mock()
4
1856
by: John J. Lee | last post by:
I'm trying define a class to act as a Mock "handler" object for testing urllib2.OpenerDirector. OpenerDirector (actually, my copy of it) does dir(handler.__class__) to find out what methods a handler supports. So, my mock class has to have appropriate methods defined on it. To avoid the hassle of manually defining lots of mock classes, I wanted to have a base class MockHandler so that I could do class...
51
3038
by: Reinhold Birkenfeld | last post by:
Hello, at the moment python-dev is discussing including Jason Orendorff's path module into the standard library. Do you have any other good and valued Python modules that you would think are bug-free, mature (that includes a long release distance) and useful enough to be granted a place in the stdlib? For my part, ctypes seems like a suggestion to start with.
1
1752
by: Kutty Banerjee | last post by:
Hi, curious if there is a tool available for creating Mock Objects in C++ the JAVA style. kutty
4
3375
by: womanontheinside | last post by:
I have a library which was written in C, you call a function, it provides the result by a callback to specific function names. I am trying to wrap the calls to this inside a class, but this causes a problem with the callbacks, for example: class X { public: add(); };
4
2393
by: David Thielen | last post by:
Hi; Are there mock objects anywhere that I can use to write nunit tests for my code behind methods? -- thanks - dave david_at_windward_dot_net http://www.windwardreports.com
2
3252
by: Terry | last post by:
I'm looking at NMock2 as a framework to create mock objects during my unit testing. One part that I'm trying to understand is that it will mock interfaces, not concrete classes. I normally don't create interfaces for every class. For example I normally don't have an interface for my business object classes. A business object class is going to have too many unique methods that I don't see the point of creating an interface that will only...
5
5411
by: Pawel Pabich | last post by:
Hi, I need to mock Stream.Read method with RhinoMocks but it looks like it's impossible. Any ideas?
4
4966
intelrate
by: intelrate | last post by:
Here is a question. I wanted my JUnit tests to be more modular and found how to mock protected methods for that purpose. Briefly speaking, we cannot mock and control calls of protected method with JDK dynamic proxy because protected methods are not in any interface but we can create MockInterface with mock method under control and call it from overridden protected method to achieve our goal. The problem I’m trying to resolve now is that my...
0
9699
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9562
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10538
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10305
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10063
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9115
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5622
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2966
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.