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

Mock out dependencies while testing in Python

Rain
March 19, 2021
150

Mock out dependencies while testing in Python

Rain

March 19, 2021
Tweet

Transcript

  1. Agenda Why isolate dependencies during testing The scope of patched

    object The potential risks of mocking dependencies with mock package The tricks of MagickMock
  2. Why isolate dependencies during testing The essence of the test

    Tragedies while testing without isolation When should I mock?
  3. Otherwise… Paid for your API calls Got banned because high-frequency

    access Pollute the storage Execute unrecoverable operations
  4. Lab 01 How py te st po ll u te

    your s to rage while unit- te s ti ng?
  5. The scope of patched object Context manager Function decorator Class

    decorator Construct and destruct manually (not recommend)
  6. Lab 03 - 1 Th e unit te st was

    passed accidenta ll y (but maybe you th ink th at it should be like th is )
  7. Lab 03 - 2 In some cases, mock wi ll

    catch th e issues for you au to ma ti ca ll y
  8. Lab 03 - 3 But some ti mes it won’t,

    so we had be tt er make th e schema of patched object s tr ic te r by ourselves
  9. Lab 04 - 1 Th e valid a tt ribu

    te s of a MagicMock object
  10. Lab 04 - 2 Apply th e fi xed a

    tt ribu te s recursively via th e au to spec kwarg