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.
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♡ 1 ↻ 0Shadows on the wall parable ref! Mocks are the shadows on the wall.
What is I hear, is I should code in a cave.
♡ 0 ↻ 0Good practices
- minimize mocking
- use a library instead of your handrolled
- complex mocking means you should refactor♡ 0 ↻ 0MagicMock means dict, list, str behavior* is mocked automatically. (Confusing why we would ever not use MagicMock)
* dunder methods
♡ 0 ↻ 0@patch autospec helps avoid mocking methods that don't exist. I think.
♡ 0 ↻ 0@patch Code for the talk: https://github.com/liz-acosta/testing-strategies-for-python
♡ 2 ↻ 1@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`♡ 0 ↻ 0@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.♡ 0 ↻ 0@patch Audience question: how do I encourage the team to do unit tests?
speaker: unit testing something you can do now or you can do emergency app support at 3am
audience question: how do you know if you are mocking too much?
speaker: 2+ maybe
me: 5+, or if it is easy to use the real thing (file system is best example)♡ 0 ↻ 0