mistersql,
@mistersql@mastodon.social avatar

Gotta catch up on mocking with #python at #pyconus
When I ask the bots to write unit tests, they use ALL the mocking libraries,

The theme at the talk is mocking using unittest the built in way.

mistersql,
@mistersql@mastodon.social avatar

What's a good unit test?

  • One assertion at at time -- eh, maybe.
  • Independent/isolated/controlled <-- mocking's role
  • Relevant/Meaningful <--- hard
  • Repeatable/deterministic <-- also mocking's role
  • Automatic <- CI all the things
  • Descriptive <-- long names
mistersql,
@mistersql@mastodon.social avatar

Shadows on the wall parable ref! Mocks are the shadows on the wall.

What is I hear, is I should code in a cave.

mistersql,
@mistersql@mastodon.social avatar

Good practices

  • minimize mocking
  • use a library instead of your handrolled
  • complex mocking means you should refactor
mistersql,
@mistersql@mastodon.social avatar

MagicMock means dict, list, str behavior* is mocked automatically. (Confusing why we would ever not use MagicMock)

  • dunder methods
mistersql,
@mistersql@mastodon.social avatar

If the mock doesn't work
@patch@mastodon.social("library.thing") then try
@patch@mastodon.social("my_code_where_i_import.library.library.thing")

The other trick is to switch to
import library as lib instead of from library import thing

mistersql,
@mistersql@mastodon.social avatar

@patch autospec helps avoid mocking methods that don't exist. I think.

mistersql,
@mistersql@mastodon.social avatar
mistersql,
@mistersql@mastodon.social avatar

@patch Audience question: why/when use doctest? Simple scenarios.
Agreed.
Audience question: can you mock self?
me: don't mock yourself, it is bad for self-esteem.
me serious: sounds like how you'd test metaprogramming - I've never seen ordinary tests mock the self

mistersql,
@mistersql@mastodon.social avatar

@patch Audience question: Do mocks have use outside of unittesting?
speaker: maybe in an integration testing scenario with some bits mocked, e.g. a webserver with fake parts
me: Finding a mocking library outside of testing code would be astonishing. I'd want to see comments in such code to justify it.

khalidabuhakmeh,
@khalidabuhakmeh@mastodon.social avatar
  • All
  • Subscribed
  • Moderated
  • Favorites
  • python
  • DreamBathrooms
  • InstantRegret
  • thenastyranch
  • magazineikmin
  • ethstaker
  • rosin
  • mdbf
  • khanakhh
  • Youngstown
  • slotface
  • everett
  • kavyap
  • Durango
  • osvaldo12
  • JUstTest
  • ngwrru68w68
  • normalnudes
  • tacticalgear
  • modclub
  • tester
  • GTA5RPClips
  • cisconetworking
  • cubers
  • anitta
  • megavids
  • Leos
  • provamag3
  • lostlight
  • All magazines