154
edits
(→Shared functions: "between" is only for two) |
(slight rearrangement to incorporate the "%!test fail" syntax for including errors and warnings in test blocks.) |
||
Line 89: | Line 89: | ||
=== Assert === | === Assert === | ||
{{codeline|%!assert}} lines are the simplest tests to write and also the most | {{codeline|%!assert}} lines are the simplest one-line tests to write and also | ||
common: | the most common: | ||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
Line 101: | Line 101: | ||
an Octave function that throws an error when two arguments fail to compare. | an Octave function that throws an error when two arguments fail to compare. | ||
=== Test === | === 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"> | |||
%!error foo () # test that causes any error | |||
%!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 | |||
%!warning foo () # test that causes any warning | |||
%!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> | |||
These are actually shorthand versions of | |||
{{codeline|%!test fail ("foo()", "error message")}} and {{codeline|%!test fail ("foo()", "warning", "warning message")}}, where {{codeline|%!fail}} returns true if the supplied code returns an error or warning. | |||
=== Test Blocks=== | |||
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}}, {{codeline|%!error}}, and {{codeline|%!warning}} 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}}: | ||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
Line 124: | Line 143: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== | ==== Test for failure ==== | ||
If a warning or error message cannot be tested with one of the single-line tests mentioned above, the {{codeline|fail}} function can be used within a test block verify expected error and warning functionality such as: | |||
{{codeline| | |||
<syntaxhighlight lang="Octave"> | <syntaxhighlight lang="Octave"> | ||
%! | %!test | ||
%! | %! a = [1 2 3]; | ||
%! | %! b = [1 2]; | ||
%! fail ("a + b", "nonconformant arguments") | |||
%! | %!test | ||
%! | %! a = 111; | ||
%! | %! b = 112; | ||
%! fail ("['foo', a, b]", "warning", "implicit conversion from numeric to char") | |||
</syntaxhighlight> | </syntaxhighlight> | ||
The tests above pass if the errors/warnings occur as expected. | |||
=== Shared functions === | === Shared functions === | ||
Line 161: | Line 181: | ||
%! endfor | %! endfor | ||
</syntaxhighlight> | </syntaxhighlight> | ||
[[Category:Testing]] | [[Category:Testing]] | ||
[[Category:Development]] | [[Category:Development]] |
edits