My favorites | Sign in
Project Logo
                
Search
for
Updated Jun 16, 2009 by szczepiq
Labels: Featured
MockitoForPython  

Mockito-Python:

  when(mock).getStuff().thenReturn("stuff")
  when(mock).getStuff().thenRaise(Exception("foo"))

  mock.getStuff()
  mock.doSomething()

  verify(mock).doSomething()

Sources were moved to bitbucket!

Direct download link: http://bitbucket.org/szczepiq/mockito-python/get/tip.zip

Features:

- classmethods and staticmethods support using the same API:

  when(MyClass).method().thenReturn("value")

- module level functions support

  when(os.path).exists("/my/path").thenReturn(True)

- matchers support for stubbing and verifying with sample basic matchers provided

  when(mock).foo(any()).thenReturn(1)
  verify(mock).foo(contains("foo"))
  verify(mock).bar(any(int), "bar")

- consecutive stubbing API

   when(mock).getStuff().thenReturn("foo").thenReturn("bar")
   when(mock).doSomething().thenRaise(Exception("foo")).thenReturn("bar")

- no more interactions verification

  mock.foo()
  mockTwo.bar()
    
  verify(mock).foo()
  verify(mockTwo).bar()
  verifyNoMoreInteractions(mock, mockTwo) 

- flexible verification

  verify(mock, times=3).foo()
  verify(mock, atleast=2).foo()
  verify(mock, atmost=4).foo()
  verify(mock, between=[2, 4]).foo()

Some history:

Motivated by a recent release of Mox I started hacking Python version. It seems Python does not have a decent Test Spy framework so I decided to close this gap.

Do you know Python very well? Are you also keen on mocking? Start helping us by joining mockito-python mailing list!


Comment by Krzysztof.Goj, Dec 02, 2008

You should take a look at Mock library: http://www.voidspace.org.uk/python/mock.html

Comment by joel.rosdahl, Jan 09, 2009

I like Python Mocker: http://labix.org/mocker

Comment by joel.rosdahl, Feb 21, 2009

Note that the sources have been moved to BitBucket?: http://www.bitbucket.org/szczepiq/mockito-python/

(I guess the page should be updated.)

Comment by immouk, Jun 16, 2009

I used Mockito for Java before so I was glad to see that there is a version for python as well. I have however a problem with it.

e.g. when(os.path).exists('/usr/lib/foo').thenReturn(True) when(os.path).exists('/usr/lib/bar').thenReturn(False)

when you call os.path.exists('/usr/lib/foo') None is returned. It only remembers the last when().thenReturn() statement. With Mockito for Java you could set all your cases and then run the tests. Sometimes a method will call a method you want to mock several time with different argument. You couldn't use Mockito for Python for that. Am I doing something wrong?

Comment by hltbra, Jun 21, 2009

immouk, it seems to be a bug! If you try with a Mock object, it works fine. os.path is a module, and modules are treated differently, maybe it is due to this "differentiation".

Comment by kris.karczmarczyk, Jun 24, 2009

Sign in to add a comment
Hosted by Google Code