Save 37% off PRO during our Black Friday Sale! »

Neil Chazin - Strategies for testing Async code

Neil Chazin - Strategies for testing Async code

Testing code is important. Testing, primarily unit-testing async code requires heading off the the standard roadway of unit testing in python. This talk will provide a map to help you along the new path towards testing async code.

Topics include:

a brief intro to asyncio and challengs in testing with it
running coroutines (and other awaitables) under test
mocking coroutines
testing "main" asyncio loops

https://us.pycon.org/2019/schedule/presentation/215/

53b37e14a09c5a718a39fda61fe1b8e5?s=128

PyCon 2019

May 05, 2019
Tweet

Transcript

  1. Strategies For Testing Async Code Neil Chazin

  2. Who am I? — Programming in Python since the early

    2000s — Believe that testing software is important — @neilathotep neil chazin - @neilathotep
  3. Work — Currently Senior Software Engineer/Tech Lead at Agari —

    Agari secures digital communications to ensure humanity prevails over evil neil chazin - @neilathotep
  4. Brief Outline 1. Why? 2. Quick intro to async &

    asyncio 3. Challenges of testing — Some solutions I've found 4. Wrap up neil chazin - @neilathotep
  5. Why? neil chazin - @neilathotep

  6. — Testing is important — Performance is often important neil

    chazin - @neilathotep
  7. neil chazin - @neilathotep

  8. Async in Python A very brief intro neil chazin -

    @neilathotep
  9. General Async Basics — Concurrency through cooperation — yield control

    when 'waiting' - asynchronous results neil chazin - @neilathotep
  10. asyncio — framework for asynchronous computing — available in stdlib

    as of 3.4 — Improved syntax in 3.5 — Incremental changes in 3.6 and 3.7 neil chazin - @neilathotep
  11. Two primary concepts — Coroutines: perform asynchronous work — Event

    loops: schedule asynchronous work neil chazin - @neilathotep
  12. Key syntax — async - define a coroutine — await

    - 'call' a coroutine neil chazin - @neilathotep
  13. Example use case We need something simple... neil chazin -

    @neilathotep
  14. neil chazin - @neilathotep

  15. neil chazin - @neilathotep

  16. neil chazin - @neilathotep

  17. neil chazin - @neilathotep

  18. neil chazin - @neilathotep

  19. Challenge Testing a coroutine neil chazin - @neilathotep

  20. Call as a function neil chazin - @neilathotep

  21. Call as a function neil chazin - @neilathotep

  22. Tests pass but... neil chazin - @neilathotep

  23. Tests pass but... neil chazin - @neilathotep

  24. Await the coroutine neil chazin - @neilathotep

  25. SyntaxError neil chazin - @neilathotep

  26. Run in an event loop neil chazin - @neilathotep

  27. Run in an event loop neil chazin - @neilathotep

  28. Success neil chazin - @neilathotep

  29. Use pytest-asyncio — pip install pytest-asyncio neil chazin - @neilathotep

  30. neil chazin - @neilathotep

  31. neil chazin - @neilathotep

  32. neil chazin - @neilathotep

  33. neil chazin - @neilathotep

  34. Python 3.7 New function asyncio.run() simplifies things neil chazin -

    @neilathotep
  35. neil chazin - @neilathotep

  36. Challenge Mocks and patching neil chazin - @neilathotep

  37. Mocks o!en relied upon — Unittest.mock.Mock won't mock a coroutine

    neil chazin - @neilathotep
  38. neil chazin - @neilathotep

  39. neil chazin - @neilathotep

  40. neil chazin - @neilathotep

  41. neil chazin - @neilathotep

  42. neil chazin - @neilathotep

  43. Challenge Context managers neil chazin - @neilathotep

  44. neil chazin - @neilathotep

  45. Challenge More complex use cases neil chazin - @neilathotep

  46. — Previous patterns all work great but some short comings

    I've found: — test coroutines that do not run in the MainThread when executing — test syncronous functions that call into event loops () — functional tests — Event Loop variants (e.g. uvloop) neil chazin - @neilathotep
  47. neil chazin - @neilathotep

  48. neil chazin - @neilathotep

  49. neil chazin - @neilathotep

  50. neil chazin - @neilathotep

  51. Calling across threads neil chazin - @neilathotep

  52. neil chazin - @neilathotep

  53. neil chazin - @neilathotep

  54. neil chazin - @neilathotep

  55. neil chazin - @neilathotep

  56. neil chazin - @neilathotep

  57. neil chazin - @neilathotep

  58. Wrap up — Try out asyncio programming — Keep testing

    — Find sample code and presentation at: h!ps://bit.ly/nchazin-pycon2019 neil chazin - @neilathotep