Writing tests for pure functions

Learning Goals

  • Given a pure function, write a test for it.
  • Explain why pure functions are easier to test than functions with side effects.

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

This is a Pick the correct items connect. Examine the code snippets. Note down which are pure functions.

Concrete: Trig math

Use the cyber-dojo starting point.

Reflect

Ask ‘how does it feel’. Fist to five vote: 5 fingers = very, zero = not at all (or use emojis)

  • 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:

  1. You can see all the inputs in the argument list
  2. The execution is deterministic (same input produces same output)
  3. 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?

Ask ‘how does it feel’. Fist to five vote: 5 fingers = very, zero = not at all (or use emojis)

  • How much of the time do you find yourself writing tests for functions that have all these three properties?