Successfully reported this slideshow.
Your SlideShare is downloading. ×

Pretenders talk at PyconUK 2012

Ad

Pretenders
(fake servers for testing)

 Don't GET (nor POST) me wrong




                             Carles Barrobés - @...

Ad

Motivation
We want to test applications that rely on
external network services.

Are we sending the right stuff?
Do we dea...

Ad

Main Use Cases
HTTP:
Applications that call external HTTP APIs
AJAX client code


SMTP:
Applications that send e-mail

Ad

Ad

Ad

Ad

Ad

Ad

Check these out next

1 of 9 Ad
1 of 9 Ad

Pretenders talk at PyconUK 2012

Download to read offline

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

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

Advertisement
Advertisement

More Related Content

Advertisement

Pretenders talk at PyconUK 2012

  1. 1. Pretenders (fake servers for testing) Don't GET (nor POST) me wrong Carles Barrobés - @technomilk
  2. 2. Motivation We want to test applications that rely on external network services. Are we sending the right stuff? Do we deal properly with different types of responses?
  3. 3. Main Use Cases HTTP: Applications that call external HTTP APIs AJAX client code SMTP: Applications that send e-mail
  4. 4. Design Decisions Preset/Replay/Verify mocking pattern Mock at the "wire protocol" level Simplicity of client test code Configuration and checks via a ReST API • Usable in polyglot environments
  5. 5. Preset/Replay/Verify We pre-program our pretend server with pre- canned responses (presets) We let the test run, and the mock server replays the presets Finally, we verify what requests/data were sent to the server
  6. 6. Mock at wire protocol level Instead 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 mock server:port instead of the real service
  7. 7. How it all works together Start 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 results The boss takes care of cleaning up stale pretenders
  8. 8. Sample client test code from pretenders.http.client import HttpMock mock = HttpMock('localhost', 8000) #boss host and port # Define presets mock.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 it set_service_url(mock.pretend_access_point) … run stuff # Verify requests your code made r = mock.get_request(0) assert_equal(r.method, 'GET') assert_equal(r.url, '/weather?city=barcelona')
  9. 9. THANK YOU! Pretenders was written by: Alex Couper and Carles Barrobés $ pip install pretenders Docs: http://pretenders.readthedocs.org Code: https://github.com/txels/pretenders

×