Upgrade to Pro — share decks privately, control downloads, hide ads and more …

API data stubbing done right

API data stubbing done right

Talk for BrewBox Cremona about the idea behind a better way to stub data and write tests.

Avatar for Paolo Ferretti

Paolo Ferretti

June 28, 2018
Tweet

More Decks by Paolo Ferretti

Other Decks in Programming

Transcript

  1. WHO AM I ➤ Paolo Ferretti ➤ Senior software developer

    @ R&D team in MailUP ➤ @paoferretti ➤ https://github.com/pferretti
  2. import responses import requests @responses.activate def test_simple(): responses.add(responses.GET, 'http://twitter.com/api/1/foobar', json={'error':

    'not found'}, status=404) resp = requests.get('http://twitter.com/api/1/foobar') assert resp.json() == {"error": "not found"}
  3. MOCKER ➤ HTTP server ➤ Command line tool ➤ Multiplatform

    ➤ Useful proxy mode ➤ Built for containers ➤ Fast! Photo by Jim Bahn Available at https://github.com/theghostwhocodes/mocker-go https://hub.docker.com/r/theghostwhocodes/mocker/
  4. A FIRST EXAMPLE - SIMPLE.GET.JSON { "request": { "method": "GET"

    }, "response": { "headers": { "Content-Type": ["application/json"] }, "body": { "message": “Hello BrewBox!” }, "status": 200 } }
  5. A SECOND EXAMPLE - SIMPLE.POST.JSON { "request": { "method": "POST",

    "payload": { "param1": ["value1"], "param2": ["value2"] } }, "response": { "headers": { "Content-Type": ["application/json"] }, "body": { "key": "simple.POST.twoparams.json" } } }
  6. WHY IS IT IMPORTANT TO STUB DATA? ➤ Separation of

    concerns / decoupling ➤ Software development (thanks to proxy mode) ➤ Consistent data for tests ➤ Safe environments for demo ➤ Stubs are simple text files ➤ Test data more accessible for non technical people