I'm often asked the question "what unit testing framework do you use?" The answer is usually I don't, but I do use a form of micro testing that is built into Erlang.
In Erlang, every assignment of the form Lhs = Rhs where the Lhs is a ground-term and Rhs is a non-ground term can be viewed as an assertion, or unit test, since it can possibly fail.
So when we write:
{ok, S} = file:open("filename", [read])
We're writing assertion to the effect that opening "filename" for read will succeed.
So how do I write unit tests?
To answer this question, I'll walk you through how I'd write the code for an efficient Fibonacci function.
I'm actually following the three rules of TDD so Uncle Bob and the agile crowd should approve of this method ...
I'll show you the order in which I implement the code. Often we show the final version of some code, but not the order in which the code was written. This time I'm going to show the precise order in which I wrote the code, and show how and when I tested and ran the code.
I'll start by defining a module, with a unit test.
Step 1) First write a micro-unit test:
-module(fib).
-compile(export_all)
test() ->
0 = fib(1),
1 = fib(2),
2 = fib(3),
6765 = fib(20),
ok.
Where did I get these values from? - I checked on the wikipedia - I was unsure if the Fibonacci series starts 0,1,1,2,.. or 1,1,2,3.
This code won't compile correctly, since the fib function is missing.
This code won't compile correctly, since the fib function is missing.
Step 2) Write the fib function:
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
This version of the Fibonacci function is recursive and very inefficient. But I'll implement it first, because I have high confidence that the code is correct, and because I'll use it later to test the efficient version of the code.
Step 3) I compile and test the module
The module now looks like this:
-module(fib).
-compile(export_all).
test() ->
0 = fib(0),
1 = fib(1),
1 = fib(2),
6765 = fib(20),
ok.
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
I compile and test it:
1> c(fib).
{ok,fib}
2> fib:test().
ok
So now I have something that works.step 4) Add unit tests for fastfib
test/0 looks like this:
test() ->
0 = fib(0),
1 = fib(1),
1 = fib(2),
6765 = fib(20),
0 = fastfib(0),
1 = fastfib(1),
1 = fastfib(2),
2 = fastfib(3),
K = fib(25),
K = fastfib(25),
ok.
Here I check that fastfib returns the same value as fib with the lines K = fib(25),
K = fastfib(25).
Step 5) Write the fastfib function.The entire module looks like this:
-module(fib).
-compile(export_all).
test() ->
0 = fib(0),
1 = fib(1),
1 = fib(2),
6765 = fib(20),
0 = fastfib(0),
1 = fastfib(1),
1 = fastfib(2),
K = fib(25),
K = fastfib(25),
ok.
fib(0) -> 0;
fib(1) -> 1;
fib(N) -> fib(N-1) + fib(N-2).
fastfib(0) -> 0;
fastfib(N) -> fastfib(N, 1, 0).
fastfib(1, A, _) -> A;
fastfib(N, A, B) -> fastfib(N-1, A+B, A).
Step 6) Compile and test.
I compile and test the module, as in Step 3)
Step 7) I change the exports of the module and change
-compile(export_all). to -export([test/0, fib/1]).
I rename fastfib to fib and fib to slowfib.
Done
Step 8) Quickcheck
I don't have John Hughes Quickcheck on my machine, but If I did, I could write a test case that said "forall integer N >= 0, fib(N) and fastfib(N) compute the same value" - quickcheck would then generate zillions of tests that test this property.
Finally
I have used the convention of exporting a function test/0 in several modules. I also have a simple program which checks a large number of modules. It checks if the module exports the function test/0, and if so evaluates (catch Mod:test()) if this returns ok, then the module has passed its test. If not I print an appropriate error.
Note how when I wrote the module I wrote a test case, then implemented the function it was testing, then wrote another test case, then more code etc. This way I'm interleaving writing test cases with implementing the test case. This way I write the code in a number of small steps, and if something goes wrong I can just reverse the last step.
When I'm finished with the code all the test cases are ready - so I don't write the code then the test cases, I interleave the two.
This is my micro-lightweight unit test framework
This is what I use for my hobby-hacks. For paid work I use the OTP test server.