Thursday, March 6, 2014

Erlang Unit Testing

Erlang Unit Testing is really simple. There is a system already built into Erlang OTP called EUnit. The documentation can be found here:

A couple of notes on usage:

1) Tests are any function that has a _test at the end of its name. So a test function might look like:

create_user_name_test() -> "JR" = (user:create("JR"))#user.name.

2) The tests that are created do NOT have to be exported. They'll be exported automatically by Erlang.

3) To run unit tests from the shell, simply do the following:

> eunit:test( ModuleName ). 

And that's it. Thus far this is what I've been using to unit test my code. It seems to work pretty well, although it is causing some conflicts with my Makefile every now and then. No big deal. Another difference I noticed about Erlang Unit Testing vs. something like gtest or UnitTest++ for C++ is the lack of checker macros. There doesn't appear to be much use for stuff like CHECK_EQUAL( , ); the reason for this is Erlang's natural ability to do pattern matching on items that are called. In C++ you can't do the following:

13 = callingMyCppFunction();

That doesn't make sense in C++. In Erlang it makes perfect sense:

13 = calling_my_erlang_function().

And the test would succeed if calling_my_erlang_function() returns 13. Why add checker macros if your language already implicitly supports a similar feature? 

No comments:

Post a Comment