Your SlideShare is downloading. ×
Pretenders talk at PyconUK 2012
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Pretenders talk at PyconUK 2012

3,014
views

Published on

Pretenders are fake servers for testing purposes. They are used to mock external servers your code interacts with (such as HTTP APIs or SMTP servers). …

Pretenders are fake servers for testing purposes. They are used to mock external servers your code interacts with (such as HTTP APIs or SMTP servers).

This is an open source project, source code is in http://github.com/txels/pretenders

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
3,014
On Slideshare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
2
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Pretenders(fake servers for testing) Dont GET (nor POST) me wrong Carles Barrobés - @technomilk
  • 2. MotivationWe want to test applications that rely onexternal network services.Are we sending the right stuff?Do we deal properly with different types ofresponses?
  • 3. Main Use CasesHTTP:Applications that call external HTTP APIsAJAX client codeSMTP:Applications that send e-mail
  • 4. Design DecisionsPreset/Replay/Verify mocking patternMock at the "wire protocol" levelSimplicity of client test codeConfiguration and checks via a ReST API• Usable in polyglot environments
  • 5. Preset/Replay/VerifyWe pre-program our pretend server with pre-canned responses (presets)We let the test run, and the mock serverreplays the presetsFinally, we verify what requests/data were sentto the server
  • 6. Mock at wire protocol levelInstead of "patch-style" mocking• Usable for testing non-Python applications• Usable in integration tests (where you cannot patch running code)Just make your application point to the mockserver:port instead of the real service
  • 7. How it all works togetherStart the main boss server (i.e. manually)Each time you create a Mock, it:• Tells the boss to instantiate a new mock server (pretender)• Returns the port of the new server• Your mock acts as a proxy to the remote server for configuration and collection of resultsThe boss takes care of cleaning up stale pretenders
  • 8. Sample client test codefrom pretenders.http.client import HttpMockmock = HttpMock(localhost, 8000) #boss host and port# Define presetsmock.when(GET /hello).reply(Hello)mock.when((POST|PUT) /somewhere).reply(status=400)mock.reply({"temperature": 23}, headers={Content-Type:application/json})# Set your app to the fake URL, and exercise itset_service_url(mock.pretend_access_point)… run stuff# Verify requests your code mader = mock.get_request(0)assert_equal(r.method, GET)assert_equal(r.url, /weather?city=barcelona)
  • 9. THANK YOU!Pretenders was written by:Alex Couper and Carles Barrobés$ pip install pretendersDocs: http://pretenders.readthedocs.orgCode: https://github.com/txels/pretenders