Pro Yearly is on sale from $80 to $50! »

Cutting off the Internet: Testing Applications that Use Requests

Cutting off the Internet: Testing Applications that Use Requests

A brief and opinionated view of testing applications and libraries that use requests by a core-developer of requests. You will receive an overview of testing with responses, vcr, httpretty, mock, and betamax. (Video: https://www.youtube.com/watch?v=YHbKxFcDltM)

C148356d89f925e692178bee1d93acf7?s=128

Ian Cordasco

April 14, 2015
Tweet

Transcript

  1. @sigmavirus24 c u t t i n g Testing Applications

    that Use Requests o f f the Internet @sigmavirus24
  2. @sigmavirus24 requests flake8 toolbelt urllib3 twine github3.py chardet sqlobject python-guide

    OpenStack
  3. None
  4. @sigmavirus24 Why give this talk?

  5. @sigmavirus24 Why give this talk?

  6. @sigmavirus24 r e v i e w

  7. @sigmavirus24 Unit of Code

  8. @sigmavirus24 Unit Test

  9. @sigmavirus24 Collaborators

  10. @sigmavirus24 Isolation

  11. @sigmavirus24 class Bar(object): def __init__(self): self.foo = Foo() def baz(self,

    *args): a = self.foo.fiz() # other stuff
  12. @sigmavirus24 Integration Tests

  13. @sigmavirus24 def get_resource( session, resource, params=None, headers=None):

  14. @sigmavirus24 url = url_from(resource) kw = {‘headers’: headers, ‘params’: params}

    resp = session.get(url, **kw) if resp.ok: return resp resp.raise_for_status()
  15. @sigmavirus24 Too Simple to Test?

  16. @sigmavirus24 D O I T L I V E !

  17. @sigmavirus24 session = Session() response = get_resource( session, ‘users’) assert

    response.ok
  18. @sigmavirus24 Now do that 400 times

  19. @sigmavirus24 Ratelimits

  20. @sigmavirus24 ✓✓✓✓✓✓FFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF

    FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF FFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  21. @sigmavirus24 I m i t a t i o n

    i s t h e s i n c e r e s t f o r m o f F l a t t e r y
  22. @sigmavirus24 responses httpretty requests-mock mock

  23. @sigmavirus24 session = mock.Mock() get_resource(session, ‘users’)

  24. @sigmavirus24 (session.get. assert_called_once_with( ‘https://api.’ ‘github.com/users’, headers=None, params=None, ))

  25. @sigmavirus24 $ py.test mock_ex.py mock_ex.py ✓

  26. @sigmavirus24 sess = mock.Mock() resp = mock.Mock() resp.ok = False

    get = sess.get get.return_value = resp
  27. @sigmavirus24 get_resource(session, ‘users’) rfs = resp.raise_for_status assert rfs.called is True

  28. @sigmavirus24 $ py.test mock_ex.py mock_ex.py ✓✓

  29. @sigmavirus24 Mock

  30. @sigmavirus24 Verbose

  31. @sigmavirus24 Verbose

  32. @sigmavirus24 P r e t e n d t o

    b e T h e N . S . A .
  33. @sigmavirus24 vcr.py betamax

  34. @sigmavirus24 vcr.py betamax

  35. @sigmavirus24 vcr.py

  36. @sigmavirus24 session = requests.Session() cassette = ‘vcrexample.yml’ with vcr.use_cassette(cassette): resp

    = get_resource( session, ‘users’) assert resp.ok users = resp.json() assert len(users) > 0
  37. @sigmavirus24 session = requests.Session() cassette = ‘vcrexample.yml’ with vcr.use_cassette(cassette): resp

    = get_resource( session, ‘users’) assert resp.ok users = resp.json() assert len(users) > 0
  38. @sigmavirus24 session = requests.Session() cassette = ‘vcrexample.yml’ with vcr.use_cassette(cassette): resp

    = get_resource( session, ‘users’) assert resp.ok users = resp.json() assert len(users) > 0
  39. @sigmavirus24 $ py.test vcr_ex.py vcr_ex.py ✓

  40. @sigmavirus24 betamax

  41. @sigmavirus24 session = Session() rec = betamax.Betamax( session, cassette_library_dir=‘.’)

  42. @sigmavirus24 with rec.use_cassette(‘betamax’): resp = get_resource( session, ‘users’) assert resp.ok

    users = resp.json() assert len(users) > 0
  43. @sigmavirus24 $ py.test betmax_ex.py betamax_ex.py ✓

  44. @sigmavirus24 self.session = Session() self.recorder = Betamax( session) self.recorder.use_cassette( cassette_name())

    self.recorder.start()
  45. @sigmavirus24 resp = get_resource( self.session, ‘users’) assert resp.ok users =

    resp.json() assert len(users) > 0
  46. @sigmavirus24 self.recorder.stop()

  47. @sigmavirus24 vcr.py betamax

  48. @sigmavirus24 vcr.py betamax

  49. @sigmavirus24 T h e r e a l i t

    y
  50. @sigmavirus24 Two part solution

  51. @sigmavirus24 Two part solution

  52. @sigmavirus24 Mocking mock, requests-mock, responses, httpretty

  53. @sigmavirus24 Integration betamax, vcr.py

  54. @sigmavirus24 T h a n k y o u