Easy, Concurrent Temp Dirs with ExUnit โœ…

Notes

Sometimes we need a temp directory in tests.

Our first inclination might be to reach for System.tmp_dir!/0. And that works, but ExUnit has a really nice helper just for us.

If we tag our test with :tmp_dir, ExUnit will pass a temp directory in the text metadata under tmp_dir:

@tag :tmp_dir
test "we need a temp directory!", %{tmp_dir: tmp_dir} do
  # we can now read and write from `tmp_dir`
end

The cool thing is that the tmp_dir generated by ExUnit includes the module and test names โ€“ making it unique. Thus, we can continue to run our tests asynchronously without worrying that weโ€™ll accidentally clobber the directory with data across tests.

For more, check out ExUnitโ€™s docs on Tmp Dir.

Want the latest Elixir Streams in your inbox?

    No spam. Unsubscribe any time.