Difference between revisions of "Tests"

From Octave
Jump to navigation Jump to search
(=Level1= is intended for the page title)
Line 40: Line 40:
   test /full/path/to/file.cc
   test /full/path/to/file.cc

Revision as of 05:01, 30 November 2013

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 ()
%! 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