A Very Brief Introduction to Responses



A Very Brief Introduction to Responses

0 1


responses-is-great

A wee talk about the responses library.

On Github blueninj0r / responses-is-great

A Very Brief Introduction to Responses

Reality

We are all working in a massive distributed system

A Fact

We often use HTTP APIs as a channel to share data between computational nodes

An Opinion(?)

It's nice to be able to release changes quickly and with confidence

A (Partial) Solution

Write some nice automated unit and integration tests

A Common Problem

Dependencies on those other HTTP APIs:

  • Slow
  • Unpredictable
  • Costly

Responses can make this a bit better

A minimal mocking framework for the Python Requests library

https://github.com/getsentry/responses

@responses.activate
def test_my_api():
    responses.add(responses.GET, 'http://twitter.com/api/1/foobar',
                  body='{"error": "not found"}', status=404,
                  content_type='application/json')

    resp = requests.get('http://twitter.com/api/1/foobar')

    assert resp.json() == {"error": "not found"}

    assert len(responses.calls) == 1
    assert responses.calls[0].request.url == 'http://twitter.com/api/1/foobar'
    assert responses.calls[0].response.text == '{"error": "not found"}'

Yay!

For not much effort we have valuable but less expensive tests

My squad has used this for Real Work™:

  • Integration with an expensive (£££ + bytes) API
  • Logic that deals with managing different responses
  • High % test coverage that runs in <1s

Contextless Code Snippet

Contextless TeamCity Snippets

All Our Problems (Might) Be Solved

Breaking the horrible dependency on the rest of the world existing

There are other tools or practises out there:

  • Other mocking things
  • Canned responses
  • Very strict isolation of network code
  • ????

Thanks!

I'm Chris McCluskey from Travel Content Platform Squad (#tcp-public)

A Very Brief Introduction to Responses