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? 1 1766
> 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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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,...
|
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...
|
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...
|
by: Kutty Banerjee |
last post by:
Hi,
curious if there is a tool available for creating Mock Objects in C++ the
JAVA style.
kutty
|
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...
|
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
|
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...
|
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?
|
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...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |