Erlang (programming language)/Tutorials/eunit: Difference between revisions
Jump to navigation
Jump to search
imported>Eric Evers mNo edit summary |
imported>Eric Evers mNo edit summary |
||
Line 45: | Line 45: | ||
% {18,#Fun<pow.4.90957845>}, | % {18,#Fun<pow.4.90957845>}, | ||
% {19,#Fun<pow.5.90843223>}] | % {19,#Fun<pow.5.90843223>}] | ||
% | |||
% Positive assertions | |||
% | |||
% Each test function is run automatically and the | % Each test function is run automatically and the | ||
% results are returned. Six total tests are run. | % results are returned. Six total tests are run. | ||
% There are 5 assertions, and 1 negative assertion. | % There are 5 assertions, and 1 negative assertion. | ||
% | |||
% Negative assertions | % Negative assertions | ||
% | |||
% A negative assertion is where an error is expected. | % A negative assertion is where an error is expected. | ||
% If | % 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 | ||
Line 59: | Line 63: | ||
% | % | ||
% See [[Erlang_programming_language/Tutorials/Errors|Exceptions]] | % See [[Erlang_programming_language/Tutorials/Errors|Exceptions]] | ||
% for more details. | |||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
Revision as of 12:50, 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>}] % % Positive assertions % % 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 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. % An error generates an exception. An excpection is % is a type of high level error message. % % See Exceptions % for more details. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%