|
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! |
Sign in to add a comment
You should take a look at Mock library: http://www.voidspace.org.uk/python/mock.html
I like Python Mocker: http://labix.org/mocker
Note that the sources have been moved to BitBucket?: http://www.bitbucket.org/szczepiq/mockito-python/
(I guess the page should be updated.)
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?
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".
Try mockito for flex: http://bitbucket.org/loomis/mockito-flex/