04.23.07
ErUnit - Unit Testing for Erlang
So, I just wrote about my new favorite language Erlang. When learning a new language, it usually helps me to work on a simple problem I’ve solved before to understand how things work. So, I grabbed one of the coding exercises we use in recruiting at t-dub (no, I’m not going to post about them). Now, as every good xp-er, I started writing my test. Or, I should say, started looking for an xUnit framework for Erlang. Nada.
So, I wrote one myself. Test-first, I might add. (Trust me, it doesn’t get much more meta than that).
Download me. (or svn co http://codeby.bestfriendchris.com/svn/ErUnit/tags/erunit-0.1.0/)
To install, put it in the Erlang code path (on my OSX machine, it is /opt/local/lib/erlang/lib). Also, if you want to use the erlang script, add the bin folder to your PATH.
To write tests, you just need to name your test file test_whatever_you_want and add the following to the file:
-behaviour(erunit_test). -export([tests/0]).
I’d show you some more example test code, but my code highlighter doesn’t support Erlang, and I don’t really have the time to hack a new language file together for it right now. So, I’ll link directly to the test case I used in writing ErUnit: test_erunit.erl
To run tests from the command line, type:
erunit
# run tests in the “tests” folder
erunit tests
To run the test from erl, type:
erunit_suite:run()
% run tests in the “tests” folder
erunit_suite:run(”tests”)
A few more notes:
- If you use seperate folders ebin and src for your .beam and .erl files respectively, ErUnit has the convention of using ebin_test and test. Again, look at the way ErUnit itself is packaged to get the idea.
- erunit_suite will run all tests in every subfolder of the one it is run in.This allows you to have some sort of hierarchy to your code and tests.
- erunit_suite is pretty smart about loading test files. It will make sure that all of the folders the test.beam files are in are included in the code path and auto-magically handle the case where you are using seperate ebin and ebin_test folders. Subfolders within either ebin or ebin_test are not yet supported.
- You might notice that a bunch of the features of most other xUnit implementations are missing. This is, at least in some cases, on purpose (in others, it’s because we’re talking 0.1 version). I’m only adding features as I need them. For instance, I haven’t added Setup or Teardown methods, mostly because I’m not yet convinced that they will be required in a functional language. And even if they are needed, it’s pretty trivial to wrap your test functions in a setup/teardown pair of funs.
- No Documentation? Yeah. I’ve actually been sitting on ErUnit for at least a week because I wanted to write some good docs for it. Since that hasn’t happened yet, I figured I’d at least put this out there and maybe get around to it eventually.
Comments/Suggestions/Complaints?

Eugene Wallingford said,
May 10, 2007 at 2:11 pm
Strange. Twice in one day I read about unit testing frameworks for Erlang.
Have you seen:
http://pragdave.pragprog.com/pragdave/2007/04/testfirst_word_.html
How does it compare to your ErUnit?
BestFriendChris said,
May 11, 2007 at 8:18 am
I just posted a little something to hopefully show the differences. I hadn’t seen the article before, but I’m glad there’s finally a good example showing it’s use. I hadn’t seen one before.
Ben Poweski said,
July 12, 2007 at 12:34 pm
Nice, I like your xUnit framework better than eunit. If feels more natural given my Ruby background.
Keep it up!