Erlang (programming language)/Tutorials/eunit: Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
(New page: Sample program that uses eunit to test a function. -module(power). -export([pow/2, start/0]). -include_lib("../../lib/eunit/include/eunit.hrl"). % adjust your own path pow(_,0...)
 
imported>Eric Evers
No edit summary
Line 34: Line 34:
  %    3> eunit:test(pow).
  %    3> eunit:test(pow).
  %    All 6 tests successful.
  %    All 6 tests successful.
%
 
  % But how does it work?
  % But how does it work?
  % eunit:test(fact) uses fact:fact_test_() to produce  
  % eunit:test(fact) uses fact:fact_test_() to produce  
  % a list of test functions(a test suite):
  % a list of 6 test functions(a test suite):
  %
   
  %    7> fact:fact_test_().
  %    7> fact:fact_test_().
  %    {15,#Fun<pow.1.106864721>},
  %    {15,#Fun<pow.1.106864721>},
Line 45: Line 45:
  %    {18,#Fun<pow.4.90957845>},
  %    {18,#Fun<pow.4.90957845>},
  %    {19,#Fun<pow.5.90843223>}]
  %    {19,#Fun<pow.5.90843223>}]
%
 
  % Each test function is run automatically and  
  % Each test function is run automatically and the
  % results are returned.
  % results are returned. Six total tests are run.
% There are 5 assertions, and 1 negative assertion.
% Negative assertions
% A negative assertion is where an error is expected.
% If a test is generated, then the test succeeds.
% Since this power function does not know how to do
% negative bases, then it causes an error. Since an
% error is expected, the test succeeds.
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Revision as of 13:32, 2 October 2008

Sample program that uses eunit to test a function.

-module(power).
-export([pow/2, start/0]).
-include_lib("../../lib/eunit/include/eunit.hrl").      
% adjust your own path
pow(_,0) -> 1;
pow(X,1) -> X;
pow(X,N) when N>1, is_integer(N) -> 
       X * pow(X,N-1).
start() ->
       io:format(" This is a demo for eunit, the unit testing module.\n"),
       io:format(" Now testing the fact() function with eunit\n"),
       io:format(" Running: eunit:test(fact)~n"),
       eunit:test(fact).
power_test_() ->
    [?_assert(pow(0,0) == 1),
     ?_assert(pow(1,1) == 1),
     ?_assert(pow(2,2) == 4),
     ?_assert(pow(3,3) == 27),
     ?_assert(pow(4,4) == 256),
     ?_assertException(error, function_clause, pow(-1,0.5))
    ].
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note: Default unit test functions are automatically exported by eunit
%
% To test function power() we compile and test.
%
%    2> c(power).
%    3> eunit:test(pow).
%    All 6 tests successful.
 
% But how does it work?
% eunit:test(fact) uses fact:fact_test_() to produce 
% a list of 6 test functions(a test suite):

%    7> fact:fact_test_().
%    {15,#Fun<pow.1.106864721>},
%    {16,#Fun<pow.2.81990874>},
%    {17,#Fun<pow.3.122231603>},
%    {18,#Fun<pow.4.90957845>},
%    {19,#Fun<pow.5.90843223>}]
  
% Each test function is run automatically and the
% results are returned. Six total tests are run.
% There are 5 assertions, and 1 negative assertion.
% Negative assertions
% A negative assertion is where an error is expected.
% If a test is generated, then the test succeeds.
% Since this power function does not know how to do
% negative bases, then it causes an error. Since an 
% error is expected, the test succeeds.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%