Writing tests for pure functions
Session Outline
- 10 min connect: pure or not quizz
- 25 min concrete: write tests for trig math
- 5 min reflect: was this easy or hard?
- 10 min concept: pure functions are easier to test
- 5 min reflect: pure functions in your code?
Connect: Pure or not quizz
Examine the code snippets. Note down which are pure functions.
Concrete: Trig math
Use the cyber-dojo starting point.
Reflect
Fist to five vote: 5 fingers = very, zero = not at all
- How hard was getting some test coverage of this code?
- How relevant was this to your normal work?
Ask for comments: how did this feel?
Concept: Pure functions are easier to test
This code was relatively easy to get basic tests in place. Mostly small functions, but (more importantly) pure functions
An easy-to-test function has these properties:
- You can see all the inputs in the argument list
- The execution is deterministic (same input produces same output)
- You can see all the outputs in the return value
Code that is harder to test will lack some or all of these properties.
Reflect: What about your code?
Fist to five vote: 5 fingers = very, zero = not at all
- How much of the time do you find yourself writing tests for functions that have all these three properties?