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 26: | Line 26: | ||
See the file test/fntests.log for additional details. | See the file test/fntests.log for additional details. | ||
To run tests in a specific file, one can simply specify the path instead of a function name: | To run tests in a specific file, one can simply specify the path instead of a function name: | ||
Line 34: | Line 32: | ||
== Writing tests == | == Writing tests == | ||
Tests appear as <code>%!</code> blocks at the bottom of the source file, together with <code>%!demo</code> blocks. A typical m function file, will have the following structure: | Tests appear as <code>%!</code> blocks at the bottom of the source file, together with <code>%!demo</code> blocks. A typical m function file, will have the following structure: | ||
Line 93: | Line 89: | ||
=== Assert === | === Assert === | ||
{{codeline|%!assert}} lines are the simplest | {{codeline|%!assert}} lines are the simplest tests to write and also the most | ||
the most common: | common: | ||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
Line 101: | Line 97: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
These are actually a shorthand version of {{codeline|%!test assert (foo (bar))}}, and {{codeline|assert}} is simply an Octave function that throws an error when two arguments fail to compare. | These are actually a shorthand version of | ||
{{codeline|%!test assert (foo (bar))}}, and {{codeline|assert}} is simply | |||
an Octave function that throws an error when two arguments fail to compare. | |||
=== | === Test === | ||
While single {{codeline|%!assert}} lines are the most common used tests, {{codeline|%!test}} blocks offer more features and flexibility. The code within {{codeline|%!test}} blocks is simply processed through the Octave interpreter. If the code generates an error, the test is said to fail. Often {{codeline|%!test}} blocks end with a call to {{codeline|assert}}: | |||
While single {{codeline|%!assert | |||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
Line 149: | Line 124: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | === Error / Warning === | ||
It is also important to test that a function performs its checks correctly | |||
and throws errors (or warnings) when it receives garbage. This can be done with | |||
{{codeline|error}} (or {{codeline|warning}}) blocks: | |||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
%!test | %!error foo () # test that causes any error | ||
%! a | %!error <BAR must be a positive integer> foo (-1.5) # test that throws specific error message | ||
%! | %!error id=Octave:invalid-fun-call foo () # test that throws specific error id | ||
%!test | %!warning foo () # test that causes any warning | ||
%! a | %!warning <negative values might give inaccurate results> foo (-1.5) # test that triggers a specific warning message | ||
%! | %!warning id=BAR:possibly-inaccurate-result foo (-1.5) # test that triggers a specific warning id | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== Shared functions === | === Shared functions === | ||
Line 187: | Line 161: | ||
%! endfor | %! endfor | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Testing]] | [[Category:Testing]] | ||
[[Category:Development]] | [[Category:Development]] |