Editing Tests
Jump to navigation
Jump to search
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision | Your text | ||
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 Octave manual on [http://www.gnu.org/software/octave/doc/interpreter/Test-Functions.html#Test-Functions Test Functions]. | |||
== Writing tests == | == Writing tests == | ||
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. | |||
[http:// | |||
===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 | |||
%! | %! 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 {{Codeline|test}} function/command. For example, to test the {{Codeline|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 | |||