473,322 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Problem When Unit Testing with PMock

Hi,

Anyone was using pmock for unit testing with python? I met a problem
and hope someone to help me. For short, the pmock seems can not mock a
iterator object.

For example, the tested object is foo, who need to send message to
another object bar. So, to test the foo, I need mock a mockBar. But
the bar is a iterator, the foo use the bar in this way:

foo.method(self, bar):
for n in bar:
do somthing
...
my purpose is create a mockBar and pass this mock object to
foo.method(), but this ideal simply does not work. Python will complain
that calling on an uniteraterable object. I'v already set anything
like mockBar.expects(__iter__()) and mockBar.expects(next()).

Does anyone has some clues?

-
narke

Jul 18 '05 #1
3 2612

<st****@lczmsoft.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Anyone was using pmock for unit testing with python? I met a problem
and hope someone to help me. For short, the pmock seems can not mock a
iterator object.
Why bother?

def mockit(): raise StopIteration

now pass mockit()
For example, the tested object is foo, who need to send message to
another object bar. So, to test the foo, I need mock a mockBar. But
the bar is a iterator, the foo use the bar in this way:

foo.method(self, bar):
for n in bar:
do somthing
...


To properly test the method, *also* feed it an iterator that yields a few
objects of the appropriate type. Assuming n is to be an int...

def intit(k):
for i in range(k): yield i

Terry J. Reedy

Jul 18 '05 #2
st****@lczmsoft.com wrote:
Peter,

for a object foo who need a iterator to do a job, i write test to make
sure the foo correctlly used the iterator, so a simple empty iterator
is not enough. because in the case, i need to see if or not the foo
called the iterator to get the proper data from it and do the proper
translating on them. so, the mock iterator i need should be such an
iterator which can returns be iterated out a lot of test data as i
willed.
See Terry's reply which referenced my posting to better understand
the point I was making.
i guess you are not familar with unit testing.
Your guess is wrong.
we need mock objects
not because they are hard to implemented, we need them to make the
tests easy and fine.


Thanks for the tutorial. :-) I'm rather well acquainted
with unit testing and mock objects.

My point was merely that what Terry suggested did not act the
way I thought he was proposing that it acted, and I was
hoping to correct that. Not having actually read and understood
your entire original question, I didn't know at the time and
still don't know what you *really* need, just that Terry's
suggestion looked like a bit of a brain fart. :-)

For what it's worth, I tend to be of the school that says that
overuse of mock objects is a "testing smell", and as part of
acting on that belief I avoid using generic mock objects.
Instead, I write minimal ones which are carefully customized
for a particular test, to make sure that the resulting mock
object's code clearly tells a reader of the test exactly what
it is trying to accomplish. Generic mocks, on the other hand,
tend to get overused and rarely make the tests very readable,
IMHO.

-Peter
Jul 18 '05 #3
Peter,

I'm so sorry, the letter was originally wrote to Terry, not to you!
I guess Terry not very familar to unit testing because he said:

-- cut --
I am not familiar with pmock, but my impression is that mock objects
are
for objects that you may not have available, such as a connection to a
database or file or instance of an umimplemented class ;-). For such
objects, the mock-ness consists in returning canned rather than actual
fetched or calculated answers. Iterators, on the other hand, are
almost as
-- end --

and i just want to say, mocking object is a useful testing method , not
was telling a tutorial. :-)
What you said about the overuse of the generic mock object is very
interesting. I did not think that before. I believe that use generic
or customized mocks is a problem of balance, so we may call it Art :-)

Below is my code, you may look it to understand my original question
(sorry for the coding for i am totally a python newbie):

---------------------------------------
import unittest

from pmock import *

from mainctr import MainCtr

class MainCtrTest(unittest.TestCase):

def testProcess(self):

filePicker = MockFilePicker()

filePicker.expectedNextCalls = len(MockFilePicker.fns) + 1

rpt0 = "foo"

rpt1 = "bar"

(router0, router1) = (Mock(), Mock())

router0.expects(once()).feedIn(eq(MockFilePicker.f ns))

router1.expects(once()).feedIn(eq(MockFilePicker.f ns))

router0.expects(once()).deliver()

router1.expects(once()).deliver()
router0.expects(once()).getDeliveredSrcList().will (return_value(["fn0",
"fn2"]))

router1.expects(once()).getDeliveredSrcList().will (return_value([]))
router0.expects(once()).getDeliveryRpt().will(retu rn_value(rpt0))
router1.expects(once()).getDeliveryRpt().will(retu rn_value(rpt1))
routes = [router0, router1]

ctr = MainCtr(filePicker, routes)

ctr.process()

self.assertEquals(ctr.getProcessRpt(), [rpt0, rpt1])

filePicker.verify()

router0.verify()

router1.verify()

class MockFilePicker:

fns = ["f0", "f1", "f2"]

idx = 0

nextCalls = 0

resetCalls = 0

expectedResetCalls = 1

expectedNextCalls = 0

processedSet = set()

expectedProcessedSet = set(["fn0", "fn2"])

rememberProcessedCalls = 0

expectedRememberProcessedCalls = 1

def __iter__(self):

return self

def reset(self):

self.resetCalls += 1
def next(self):

self.nextCalls += 1

if self.idx < len(self.fns):

self.idx += 1

return self.fns[self.idx - 1]

else:

raise StopIteration

def processed(self, fn):

self.processedSet.add(fn)

def rememberProcessed(self):

self.rememberProcessedCalls += 1


def verify(self):

if self.nextCalls != self.expectedNextCalls:

raise Exception("nextCalls: " + str(self.nextCalls))

if self.resetCalls != self.expectedResetCalls:

raise Exception("resetCalls: " + str(self.resetCalls))

if self.processedSet != self.expectedProcessedSet:

raise Exception("processedSet: " + str(self.processedSet))

if self.rememberProcessedCalls !=
self.expectedRememberProcessedCalls:
raise Exception("rememberProcessedCalls: " +
str(self.rememberProcessedCalls))
if __name__ == "__main__":

unittest.main()

Jul 18 '05 #4

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

Similar topics

11
by: rhat | last post by:
Hi Everyone, I've recently been reading some articles about unit-testing in Python , but I am a bit confused: where do I go to get started with this? I tried googling for "unittest" but all I've...
14
by: | last post by:
Hi! I'm looking for unit-testing tools for .NET. Somthing like Java has --> http://www.junit.org regards, gicio
4
by: Peter Rilling | last post by:
Does VS.NET 2005 Professional support integrated unit testing, or is that only with the team system?
0
by: Andy | last post by:
Hi all, I'm trying to create unit tests in a seperate assembly from the library. However there are some internal classes that deserve testing but are not intended to be used by the public. ...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
7
by: Diffident | last post by:
Hello All, Can anyone please suggest me a good unit testing tool. I have seen NUnit but not sure on how I can use it to test my methods which involve session variables, viewstate variables,...
4
by: Dat AU DUONG | last post by:
Hi, I am new to Unit testing, could you tell me where I could find information (hopefully step by step) and what is the benefit of unit testing. I am a sole developer in a company, therefore I...
5
by: shuisheng | last post by:
Dear All, I was told that unit test is a powerful tool for progamming. If I am writing a GUI code, is it possible to still using unit test? I have a little experience in using unittest++. But...
2
by: mo.sparrow | last post by:
Typemock Isolator – A powerful mocking framework for .NET unit testing and Test Driven Development (in Visual Studio). http://www.typemock.com/learn_about_typemock_isolator.html NUnit - A...
0
isladogs
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...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
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...

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.