EUnit examples: Introduction
Some examples of how to use Erlang’s unit-testing framework, EUnit. Starting with simple examples and getting more advanced.
Some examples of how to use Erlang’s unit-testing framework, EUnit. Starting with simple examples and getting more advanced.
A simple EUnit test might look like this:
While you could specify each test with a single function, you can also use a test “instantiator” (or generator) and have it return a list.
What if we want to run some setup before a set of tests (and cleanup afterwards)?
We saw that setup
runs setup/cleanup before all tests in a list. If you want to run some setup/cleanup before each
test in a list, you can use foreach
:
If you want to run some setup before (and cleanup after) a list of tests, and also some setup and cleanup for each
test, you can nest setup
and foreach
:
The result from setup
is passed to cleanup
already. What if we want to pass it to each of our tests? It’s a bit
awkward, but it looks like the following:
Similar to passing the result from {setup, Setup, ...
to each test, you can also pass the result from {foreach,
Setup, ...
to each test, but there are some differences.
In several of the previous posts, for example Using ‘setup’, I’ve
started a server in suite_setup/0
and needed to kill it in suite_cleanup/1
. I showed a simple way to do that, but
it’s not the best. Here’s a better way to do it.