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('firstname.lastname@example.org', 'email@example.com', ... 'Hi there!', 'How is it going?') Called smtplib.SMTP('localhost') Called smtp_connection.sendmail( 'firstname.lastname@example.org', ['email@example.com'], 'To: firstname.lastname@example.org\nFrom: email@example.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.
I have to say, this is by far the simplest mock implementation I have ever seen, and complexity is usually the biggest factor standing in the way of greater use of mocks. It would be worth porting doctests to Java simply to enable the use of something like this. Bravo.
Also, that's the best captcha I've ever seen.# Alec Munro
Wow, that cool, and ridiculously short. I love Python.
I'm fairly new to unit tests, doctests, et al and I was having a lot of problems with my objects doing things that were difficult/impossible to capture in regular tests (kernel calls on MS Windows and printing out funky escape characters on *nix systems). This is exactly what I need.
I was wondering what the license was on minimock? Any chance adding it to the cheese shop or the cook book?
I don't feel up to adding this to the Wikipedia doctest entry myself. Maybe you could mention it in the talk section at http://en.wikipedia.org/wiki/Talk:Doctest ?