473,498 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Mock object creation by example?

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()
m.xyzzy("foo", bar=2)
m.do_nothing()

# then run the test
m.start_test()
bu = BarUser(m)
bu.method_a("foo", 1, bar=2)
bu.method_b()
That's it, essentially.

It does needs a bit more complication than that to be useful. For
example, a way specifying return values and exceptions to be expected,
a way of asking it to pretend to be a class object, ways of making
slightly fuzzy the expected number and order of calls and their
arguments, and the ability to easily inherit from Mock and override
methods etc. For example, you might say:

m.return_something().returns(1)
m.raise_something().raises(Exception)
Of course, it turns out somebody else thought of it first. For
example, in Java:

http://www.abstrakt.de/mockcreator.html
Anybody know if something like this already exists in Python?

Actually, looking at the examples on mockcreator's web page does lead
give you the suspicion that they've had some contact with a certain
language <wink>:

// setup of MockObjects
MockFoo mockFoo = new MockFoo();
mockFoo.expectDoSomething( "nobody expects", "the spanish inquisition" );

// Testcode
assertEquals( "the spanish inquisition", mockFoo.doSomething( "nobodyExpects" ) );

// Verify
mockFoo.verify();
John
Jul 18 '05 #1
0 1964

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

Similar topics

4
1844
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...
9
5788
by: Peter Hansen | last post by:
The term "mock filesystem" refers to code allowing unit or acceptance tests to create, read and write, and manipulate in other ways "virtual" files, without any actual disk access. Everything is...
1
1769
by: ischenko | last post by:
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"...
4
2366
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
3
2422
by: Joe Van Dyk | last post by:
Say I've written a class that wraps around a particular (complex) communication service library. I want to unit test objects that use that communication service. Is the "best practice" to...
2
3242
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...
1
2241
by: earthwormgaz | last post by:
Has anyone seen a tool for C++ that automatically creates mock classes? I found MockMaker for Java, but I wondered if there was something similar for us C++ developers. The idea is to read in...
0
1048
by: Fuzzyman | last post by:
Mock 0.4.0 has just been released, the first release in about ten months (but worth the wait). Mock is a simple library for testing: specifically for mocking, stubbing and patching. * Mock...
2
1578
by: mde | last post by:
I'm wondering if there is a "best practice" for *creating doctests in methods* that reduces clutter/duplication of dummy instantiations. In the following code there are five (labeled 1-5) possible...
0
7121
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,...
0
6993
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...
0
7162
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,...
0
7375
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...
0
5456
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,...
1
4899
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4584
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1411
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 ...
0
287
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...

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.