mock testing mit python

9

Click here to load reader

Upload: plone-foundation

Post on 08-May-2015

297 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Mock testing mit Python

Mock Testing mit PythonPyBonn Meetup – März 2013

Timo Stollenwerk

Page 2: Mock testing mit Python

Testing Pyramide

Page 3: Mock testing mit Python

Ein Test ist kein Unit Test wenn

● Datenbankverbindung● Netzwerkverbindung● Dateisystemzugriff● Externer Service

Page 4: Mock testing mit Python

Mocks in Python

● Mock● Mocker● pMock● pyMock● mox● ...

Page 5: Mock testing mit Python

PostMonkey

>>> from postmonkey import PostMonkey

>>> pm = PostMonkey('your_api_key')

>>> pm.ping()

u"Everything's Chimpy!"

Page 6: Mock testing mit Python

Mocker

>>> mocker = Mocker()

>>> postmonkey = mocker.replace("postmonkey")

>>> pm = postmonkey.PostMonkey(ANY)

>>> pm.ping()

>>> mocker.result(u"Everything's Chimpy!")

>>> mocker.replay()

Page 7: Mock testing mit Python

Mocker: PostMonkey

>>> from postmonkey import PostMonkey

>>> pm = PostMonkey('your_api_key')

>>> pm.ping()

u"Everything's Chimpy!"

>>> pm

<mocker.Mock object at 0x9d7624c>

Page 8: Mock testing mit Python

Mock

>>> @patch('postmonkey.PostMonkey')

>>> def test_mailchimp_ping_method(mock_class):

>>> mock_class().ping = MagicMock(

>>> return_value=u"Everything's Chimpy!")

>>> from postmonkey import PostMonkey

>>> pm = PostMonkey('123')

>>> assert pm.ping() == u"Everything's Chimpy!"

>>> test_mailchimp_ping_method()

Page 9: Mock testing mit Python

Mock Testing mit PythonPyBonn Meetup – März 2013

Timo Stollenwerk

github.com/tisto/mock-testing-in-python