One Experiment, Several Measurements

We can do better, though. We think the setup block should run an experiment, and each test block should run measurements. Combining setup code with test code makes it hard to isolate single-purpose code. The experiments and measurement concerns can become too entangled.

Let’s rewrite that:

 defmodule​ AppTest ​do
 use​ ExUnit.Case
 
  setup( context ) ​do
  context
  |> Map.put( ​:setup​, ​:done​)
 end
 
  test ​"​​setup works"​, context ​do
  assert context.setup == ​:done
 end
 end

That’s a little better. We have a setup block that can run an experiment and test code that can run measurement. That’s a potential improvement, but since there’s typically only a single setup, the improvements ...

Get Functional Programming: A PragPub Anthology now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.