95

What is your single favorite mocking library for Python?

Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
  • minimock which uses doctest – Prof. Falken Jan 25 '13 at 09:40
  • 1
    "Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with clean & simple API. Mockito doesn't give you hangover because the tests are very readable and they produce clean verification errors." https://code.google.com/p/mockito/ – mit Mar 21 '14 at 12:55

7 Answers7

43

I've only used one, but I've had good results with Michael Foord's Mock: http://www.voidspace.org.uk/python/mock/.

Michael's introduction says it better than I could:

There are already several Python mocking libraries available, so why another one?

Most mocking libraries follow the 'record -> replay' pattern of mocking. I prefer the 'action -> assertion' pattern, which is more readable and intuitive particularly when working with the Python unittest module.

...

It also provides utility functions / objects to assist with testing, particularly monkey patching.

Community
  • 1
  • 1
Daryl Spitzer
  • 143,156
  • 76
  • 154
  • 173
  • 9
    mock is now part of the Python standard library, available as unittest.mock in Python 3.3 onwards. – n611x007 May 09 '13 at 10:11
  • Do NOT use backported mock with Python 2.x! It is last updated in 2012, riddled with bugs (see the issues on google code!). Stay away. – Andriy Drozdyuk Mar 19 '15 at 22:22
8

Mox, from Google

John Millikin
  • 197,344
  • 39
  • 212
  • 226
5

Mocker from Gustavo Niemeyer.

It's not perfect, but it is very powerful and flexible.

ddaa
  • 52,890
  • 7
  • 50
  • 59
5

I'm the author for mocktest. I think it's pretty fully featured and easy to use, but I might be biased:

http://gfxmonk.net/dist/doc/mocktest/doc/

gfxmonk
  • 8,614
  • 5
  • 42
  • 53
4

Dingus, by Gary Bernhardt.

John Eikenberry
  • 1,133
  • 12
  • 12
3

pyDoubles the test doubles framework for Python, by iExpertos.com. It supports mocks, stubs, spies and matchers, including Hamcrest matchers

Carlos Ble
  • 31
  • 1
0

I've used pMock in the past, and didn't mind it, it had pretty decent docs too. However, Foord's Mock as mentioned above is also nice.

Community
  • 1
  • 1
William Keller
  • 5,256
  • 1
  • 25
  • 22
  • 8
    That's what the pMock's author says about pMokc: "_Just to note that pMock is no longer in development (really must update the web page), actually it was more an experiment I finished playing with about four years ago and assumed the "alpha" designation would scare people away._" [http://www.brunningonline.net/simon/blog/archives/002262.html] – ssoler Mar 31 '11 at 08:51