The Artima Developer Community
Sponsored Link

Python Buzz Forum
minimock

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ian Bicking

Posts: 900
Nickname: ianb
Registered: Apr, 2003

Ian Bicking is a freelance programmer
minimock Posted: Jan 7, 2007 7:46 PM
Reply to this message Reply

This post originated from an RSS feed registered with Python Buzz by Ian Bicking.
Original Post: minimock
Feed Title: Ian Bicking
Feed URL: http://www.ianbicking.org/feeds/atom.xml
Feed Description: Thoughts on Python and Programming.
Latest Python Buzz Posts
Latest Python Buzz Posts by Ian Bicking
Latest Posts From Ian Bicking

Advertisement

At the last ChiPy meeting there was a presentation by Fawad Halim on the python-mock mock object library.

While I usually abhor complex mock libraries, that one actually looked quite good and sufficiently simple. But I also thought it could be a lot simpler using doctest.

So after the meeting I whipped up minimock. Here's what a test looks like; first some setup:

>>> smtplib.SMTP = Mock('smtplib.SMTP')
>>> smtplib.SMTP.mock_returns = Mock('smtp_connection')

Then the test:

>>> send_email('ianb@colorstudy.com', 'joe@example.com',
...            'Hi there!', 'How is it going?')
Called smtplib.SMTP('localhost')
Called smtp_connection.sendmail(
    'ianb@colorstudy.com',
    ['joe@example.com'],
    'To: joe@example.com\nFrom: ianb@colorstudy.com\nSubject: Hi there!\n\nHow is it going?')
Called smtp_connection.quit()

It's about 25 lines of actual code and I believe supports all the patterns that python-mock supports. Almost all features are actually implicit features of doctest. You really want to set the ELLIPSIS option (which I prefer to use in all my doctests).

Why people don't implement doctest in other language, I have no idea. It's totally translatable and it's so damn simple. You don't have to understand all sorts of ideas; the ideas just fall out of doctest (like this mocking). Maybe it's too simple for anyone to feel proud of porting it. It's not a whole new concept in testing (*coughyagnicough*). It's just a simple idea that, once you figure it out, feels like it should have been obvious all along.

Read: minimock

Topic: Code Craft Previous Topic   Next Topic Topic: [Jan 3, 2007 02:49 PST] 1 Links

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use