from mock import Mock      mocking for python
Mocking >>> from mock import Mock >>> Mock() <Mock id=4419885840>Calling an undefined method on the mock            returns...
Mocking>>> m = Mock(return_value=foo)>>> m()foo       Mocks can have return values.
Mocking>>> m(bar, answer=42)foo>>> m.calledTrue>>> m.call_argscall(bar, answer=42)>>> m.call_count1     Calls to the mock ...
Mocking>>> mock = Mock()>>> mock.side_effect = Exception(Bam)>>> mock()Traceback (most recent call last):  ...Exception: B...
Documentationhttp://www.voidspace.org.uk/python/mock/mock.html
Patching>>> class Class(object):...   def method(self):...     pass...>>> with patch(__main__.Class) as M:...   instance =...
Live example
Patching@patch(paperc.base.utils.mail.EmailMultiAlternatives)def test_html_parameter_is_optional(self, EmailMultiAlternati...
mock Library Author• Michael Foord• http://www.voidspace.org.uk/python/mock/  index.html• @voidspace
Upcoming SlideShare
Loading in...5
×

Django User Group 21.02.2012 - Mock

1,189
-1

Published on

The slides of a talk I held at the "Django User Group Meetup Berlin" on the 02-21-2012.

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,189
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
4
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Django User Group 21.02.2012 - Mock

  1. 1. from mock import Mock mocking for python
  2. 2. Mocking >>> from mock import Mock >>> Mock() <Mock id=4419885840>Calling an undefined method on the mock returns the mock.
  3. 3. Mocking>>> m = Mock(return_value=foo)>>> m()foo Mocks can have return values.
  4. 4. Mocking>>> m(bar, answer=42)foo>>> m.calledTrue>>> m.call_argscall(bar, answer=42)>>> m.call_count1 Calls to the mock can be inspected.
  5. 5. Mocking>>> mock = Mock()>>> mock.side_effect = Exception(Bam)>>> mock()Traceback (most recent call last): ...Exception: Bam>>> mock.side_effect = [3, 2, 1]>>> mock(), mock(), mock()(3, 2, 1) Mocks can have side effects.
  6. 6. Documentationhttp://www.voidspace.org.uk/python/mock/mock.html
  7. 7. Patching>>> class Class(object):... def method(self):... pass...>>> with patch(__main__.Class) as M:... instance = M()... instance.method.return_value = foo... assert Class() is instance... assert Class().method() == foo...
  8. 8. Live example
  9. 9. Patching@patch(paperc.base.utils.mail.EmailMultiAlternatives)def test_html_parameter_is_optional(self, EmailMultiAlternatives): Omitting the html parameter should send the email with plain text only. msg = Mock() EmailMultiAlternatives.return_value = msg send_email(*[email, subject, text, {}]) self.assertEquals(msg.attach_alternative.call_count, 0)
  10. 10. mock Library Author• Michael Foord• http://www.voidspace.org.uk/python/mock/ index.html• @voidspace
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×