Tests: Difference between revisions

From Octave
Jump to navigation Jump to search
(added link for octave manual)
(=Level1= is intended for the page title)
Line 1: Line 1:
Writing tests for function is an important thing that is usually overlooked. It helps a lot in preventing regression. There's a section in the [[http://www.gnu.org/software/octave/doc/interpreter/Test-Functions.html#Test-Functions|octave manual]] for it.
Writing tests for function is an important thing that is usually overlooked. It helps a lot in preventing regression. There's a section in the Octave manual on [http://www.gnu.org/software/octave/doc/interpreter/Test-Functions.html#Test-Functions Test Functions].


= Writing tests =
== Writing tests ==
== in .m files ==
== in .cc files ==
==declaring functions inside a test block==


  function experience
Octave provides for robust testing of functions.  The manual includes a section on the [http://www.gnu.org/software/octave/doc/interpreter/Test-Functions.html#Test-Functions Test Functions].  Several examples are included.
 
===Declaring Functions Inside a Test Block===
 
Octave's ''test'' function automatically inserts the contents of each test block into a function. Although it is admittedly hacky/ugly, this implies that addition functions may be declared within a test block.  For example, the empty function below named {{Codeline|experience}} is accompanied by a single test block which includes two function definitions.
 
  function experience ()
  %!test
  %!test
  %! experience_design_mat
  %! experience_design_mat
Line 24: Line 27:
  %! % endfunction: don't add it here. Let test() do it.
  %! % endfunction: don't add it here. Let test() do it.


= running tests =
== Running Tests ==
== from m files ==
 
== from .cc files ==
Tests are run tests in m-files by using the {{Codeline|test}} function/command.  For example, to test the {{Codeline|experience}} above use the command below.
You can run tests in .cc files by doing something like
 
  test experience
 
Or alternatively, use the functional form
 
  test ("experience")
 
To run tests in .cc files, the path to the file must be provided.
 
   test /full/path/to/file.cc
   test /full/path/to/file.cc

Revision as of 00:18, 25 June 2012

Writing tests for function is an important thing that is usually overlooked. It helps a lot in preventing regression. There's a section in the Octave manual on Test Functions.

Writing tests

Octave provides for robust testing of functions. The manual includes a section on the Test Functions. Several examples are included.

Declaring Functions Inside a Test Block

Octave's test function automatically inserts the contents of each test block into a function. Although it is admittedly hacky/ugly, this implies that addition functions may be declared within a test block. For example, the empty function below named experience is accompanied by a single test block which includes two function definitions.

function experience ()
%!test
%! experience_design_mat
%! experience_obs_eqs
%! assert (experience_design_mat == pi);
%! assert (experience_obs_eqs == exp(1));
%!
%! endfunction  % this is a trick.
%! % now we can declare functions to be used by the test above.
%!
%! function a = experience_design_mat 
%!     a = pi;
%! endfunction
%!
%! function b = experience_obs_eqs
%!     b = exp(1);
%! % endfunction: don't add it here. Let test() do it.

Running Tests

Tests are run tests in m-files by using the test function/command. For example, to test the experience above use the command below.

 test experience

Or alternatively, use the functional form

 test ("experience")

To run tests in .cc files, the path to the file must be provided.

 test /full/path/to/file.cc