O'Reilly logo

Functional Programming: A PragPub Anthology by Michael Swaine

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required