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

From Citizendium
Jump to navigation Jump to search
imported>Eric Evers
mNo edit summary
imported>Tom Morris
 
(14 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{subpages}}
Sample program that uses eunit to test a function.
Sample program that uses eunit to test a function.


Line 13: Line 14:
  start() ->
  start() ->
         io:format(" This is a demo for eunit, the unit testing module.\n"),
         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(" Now testing the pow() function with eunit\n"),
         io:format(" Running: eunit:test(fact)~n"),
         io:format(" Running: eunit:test(pow)~n"),
         eunit:test(fact).
         eunit:test(pow).


  power_test_() ->
  power_test_() ->
Line 26: Line 27:
     ].
     ].


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Note: Default unit test functions are automatically exported by eunit
  % Note: Default unit test functions are automatically exported by eunit
%
  %
% To test function power() we compile and test.
  % To test function power() we compile and test.
%
  %
%    2> c(power).
  %    2> c(power).
%    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(pow) uses power:power_test_() to produce  
  % a list of 6 test functions(a test suite):
  % a list of 6 test functions(a test suite):
   
   
  %    7> fact:fact_test_().
  %    7> power:pow_test_().
  %    {15,#Fun<pow.1.106864721>},
  %    {15,#Fun<pow.1.106864721>},
  %    {16,#Fun<pow.2.81990874>},
  %    {16,#Fun<pow.2.81990874>},
  %    {17,#Fun<pow.3.122231603>},
  %    {17,#Fun<pow.3.122231603>},
  %    {18,#Fun<pow.4.90957845>},
  %    {18,#Fun<pow.4.90957845>},
  %    {19,#Fun<pow.5.90843223>}]
  %    {19,#Fun<pow.5.90843223>},
 
%    {20,#Fun<pow.4.90457845>}]
  % Each test function is run automatically and the
%
  % results are returned. Six total tests are run.
  % eunit:test then runs the programs in the test suite
  % There are 5 assertions, and 1 negative assertion.
% and reports the results.
  %  
% Note: The numbers [15,16,17,18,19,20] are
% the line numbers of the source code that generated the
% test functions.
%
% Assertions
%
% Six total tests are run. An assertion is a positive assertion
  % and expects the answer to be true. There are 5 positive assertions  
% and 1 negative assertion in the test suite.
%
  % Negative assertions
  % Negative assertions
%
  % A negative assertion is where an error is expected.
  % A negative assertion is where an error is expected.
  % If a test is generated, then the test succeeds.
  % If an error is generated, then the test succeeds.
  % Since this power function does not know how to do
  % Since this power function does not know how to do
  % negative bases, then it causes an error. Since an  
  % negative bases, then it causes an error. Since an  
  % error is expected, the test succeeds.
  % error is expected, the test succeeds.
  % An error generates an exception. An excpection is
%
%    assertException(error, function_clause, pow(-1,0.5))
%
% is an example of a negative assertion.
  % An error generates an exception. An exception is
  % is a type of high level error message.  
  % is a type of high level error message.  
  %
  %
  % See [[Erlang_programming_language/Tutorials/Errors|Exceptions]]
  % See [[Erlang_programming_language/Tutorials/Errors|Exceptions]]
% for more details.
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Unit tests as internal documentation
Unit tests provide a useful method of internal documentation that is
often easy to maintain. The maintenance of documentation can often
be a problem as programs evolve over time.
As long as the unit test works, the unit test serves as a nice suite
of example programs that can aid comprehension. For the purpose of
internal documentation and other reasons at least one
negative assertion is included to form a complete test suite.
A negative assertion can show the limits of
the domain or the range of a function, or even its time complexity
if one includes a timeout bound on computation time.
Software engineering
Unit tests can help one keep an eye on side-effects during
program development. Fixing a bug for some input may cause a
bug with some previously working input. Unit testing can help
with the organization of a program and inspire new solutions.
Note: for the program to work you will need to install
and perhaps compile the eunit module, and set up your path
to include it.

Latest revision as of 06:07, 8 August 2009


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 pow() function with eunit\n"),
       io:format(" Running: eunit:test(pow)~n"),
       eunit:test(pow).
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(pow) uses power:power_test_() to produce 
% a list of 6 test functions(a test suite):

%    7> power:pow_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>},
%    {20,#Fun<pow.4.90457845>}]
% 
% eunit:test then runs the programs in the test suite
% and reports the results.
% 
% Note: The numbers [15,16,17,18,19,20] are
% the line numbers of the source code that generated the 
% test functions.
%
% Assertions
% 
% Six total tests are run. An assertion is a positive assertion 
% and expects the answer to be true. There are 5 positive assertions 
% and 1 negative assertion in the test suite.
%
% Negative assertions
%
% A negative assertion is where an error is expected.
% If an error 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.
%
%     assertException(error, function_clause, pow(-1,0.5))
%
% is an example of a negative assertion.
% An error generates an exception. An exception is
% is a type of high level error message. 
%
% See Exceptions
% for more details.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Unit tests as internal documentation
Unit tests provide a useful method of internal documentation that is 
often easy to maintain. The maintenance of documentation can often 
be a problem as programs evolve over time.
As long as the unit test works, the unit test serves as a nice suite 
of example programs that can aid comprehension. For the purpose of 
internal documentation and other reasons at least one 
negative assertion is included to form a complete test suite. 
A negative assertion can show the limits of
the domain or the range of a function, or even its time complexity
if one includes a timeout bound on computation time.
Software engineering
Unit tests can help one keep an eye on side-effects during 
program development. Fixing a bug for some input may cause a
bug with some previously working input. Unit testing can help
with the organization of a program and inspire new solutions.
Note: for the program to work you will need to install 
and perhaps compile the eunit module, and set up your path
to include it.