[erlang-questions] wierd outome on my match method

Richard A. O'Keefe ok@REDACTED
Mon Feb 2 02:54:42 CET 2015


On 1/02/2015, at 5:27 am, Roelof Wobben <r.wobben@REDACTED> wrote:
> 
> I do not have made any test because that topic is still not covered in chapter 2 and 3 of the programming erlang book.

There are special testing tools for Erlang, but at this stage, you
do not need them, and the book has not mentioned testing in Erlang
for the simple reason that there is nothing Erlang-SPECIFIC to say.

A test case is just a function where you know what is supposed to happen
and can tell if it did or not.

The simplest approach is to write a module db_test with a bunch of
functions called t1, t2, ... or whatever you please.  These functions
can be called from the Erlang shell:

% erl
1> c(db). % load implementation
2> c(db_test). % load tests.
3> db_test:t1(). % try test 1
...

For example, you might have

t1() ->
    Db0 = db:new(),
    {error,_} = db:read(x, Db0), % new() is empty
    Db1 = db:write(x, 1, Db0),
    {ok,1} = db:read(x, Db1),    % can read what we wrote
    {error,_} = db:read(y, Db1), % no hallucinations
    Db2 = db:write(x, 2, Db1),
    {ok,2} = db:read(x, Db2),    % write() overwrites
    Db2 = delete(y, Db2).        % no delete => no change

The name given here does not matter, as long as it's exported.


The Test-Driven Development people say that it's a good idea to write
the tests BEFORE you write the implementation.  And along the way, in
your messages, you have actually written several test cases, you just
haven't put them as functions in a file where it's easy to run them
again.








More information about the erlang-questions mailing list