Using ExUnit's test context for unique values

Notes

Sometimes we need unique values in our tests.

There are many ways to get a unique value, but I typically see people use an arbitrary value such as :test_value. Unfortunately, that can cause name collisions if those names are used in multiple tests.

But there’s a nice ExUnit trick we can use.

The test context comes with the test name (as an atom) under the test key. That name has to be unique, and it’s tied to our test. That makes it a perfect candidate for a unique value that we can use in our test.

Want the latest Elixir Streams in your inbox?

    No spam. Unsubscribe any time.