Setting up testing environment

defmodule RAETest do
  use ExUnit.Case
  doctest RAE

  test "should raise an error if ratio of grass to weeds is incorrect" do
    RAE.init()

    water = RAE.grass("scutch", "fescues")
    fertilizer = RAE.weeds("fescues", "scutch")
    assert fertilizer / water == 1
  end

  test "should raise an error if proportion of weeds in area is incorrect" do
    RAE.init()

    area = RAE.grass("fescues", muhly)
    growth_rate = RAE.weeds(muhly, "fescues")
    irrigation / growth_rate == 1
  end
end

As you can see, RAE.init() is required for each test but this is a very expensive operation. How can this duplication be avoided while still getting tests to pass?

You can use setup_all/1. But it looks like it should be on your application.ex?

setup_all do
  RAE.init()
  :ok
end
1 Like

Putting it at the top of RAETest before the first test did the trick, thanks so much! :slight_smile:

1 Like