Next: , Up: Test and Demo Functions   [Contents][Index]

B.1 Test Functions

Command: test name
Command: test name quiet|normal|verbose
Function File: test ("name", "quiet|normal|verbose", fid)
Function File: test ("name", "quiet|normal|verbose", fname)
Function File: success = test (…)
Function File: [n, nmax, nxfail, nskip] = test (…)
Function File: [code, idx] = test ("name", "grabdemo")
Function File: test ([], "explain", fid)
Function File: test ([], "explain", fname)

Perform built-in self-tests from the first file in the loadpath matching name.

test can be called in either command or functional form. The exact operation of test is determined by a combination of mode (interactive or batch), reporting level ("quiet", "normal", "verbose"), and whether a logfile or summary output variable is used.

The default mode when test is called from the command line is interactive. In this mode, tests will be run until the first error is encountered, or all tests complete successfully. In batch mode, all tests are run regardless of any failures, and the results are collected for reporting. Tests which require user interaction, i.e., demo blocks, are never run in batch mode.

Batch mode is enabled by either 1) specifying a logfile using the third argument fname or fid, or 2) requesting an output argument such as success, n, etc.

The optional second argument determines the amount of output to generate and which types of tests to run. The default value is "normal". Requesting an output argument will suppress printing the final summary message and any intermediate warnings, unless verbose reporting is enabled.


Print a summary message when all tests pass, or print an error with the results of the first bad test when a failure occurs. Don’t run tests which require user interaction.


Display warning messages about skipped tests or failing xtests during test execution. Print a summary message when all tests pass, or print an error with the results of the first bad test when a failure occurs. Don’t run tests which require user interaction.


Display tests before execution. Print all warning messages. In interactive mode, run all tests including those which require user interaction.

The optional third input argument specifies a logfile where results of the tests should be written. The logfile may be a character string (fname) or an open file descriptor ID (fid). To enable batch processing, but still print the results to the screen, use stdout for fid.

When called with just a single output argument success, test returns true if all of the tests were successful. If called with more than one output argument then the number of successful tests (n), the total number of tests in the file (nmax), the number of xtest failures (nxfail), and the number of skipped tests (nskip are returned.


test sind
PASSES 5 out of 5 tests

[n, nmax] = test ("sind")
n =  5
nmax =  5

Additional Calling Syntaxes

If the second argument is the string "grabdemo", the contents of any built-in demo blocks are extracted but not executed. The text for all code blocks is concatenated and returned as code with idx being a vector of positions of the ends of each demo block. For an easier way to extract demo blocks from files, See example.

If the second argument is "explain" then name is ignored and an explanation of the line markers used in test output reports is written to the file specified by fname or fid.

See also: assert, fail, demo, example, error.

test scans the named script file looking for lines which start with the identifier ‘%!’. The prefix is stripped off and the rest of the line is processed through the Octave interpreter. If the code generates an error, then the test is said to fail.

Since eval() will stop at the first error it encounters, you must divide your tests up into blocks, with anything in a separate block evaluated separately. Blocks are introduced by valid keywords like test, function, or assert immediately following ‘%!’. A block is defined by indentation as in Python. Lines beginning with ‘%!<whitespace>’ are part of the preceeding block.

For example:

%!test error ("this test fails!");
%!test "test doesn't fail. it doesn't generate an error";

When a test fails, you will see something like:

  ***** test error ("this test fails!")
!!!!! test failed
this test fails!

Generally, to test if something works, you want to assert that it produces a correct value. A real test might look something like

%! a = [1, 2, 3; 4, 5, 6]; B = [1; 2];
%! expect = [ a ; 2*a ];
%! get = kron (b, a);
%! if (any (size (expect) != size (get)))
%!   error ("wrong size: expected %d,%d but got %d,%d",
%!          size (expect), size (get));
%! elseif (any (any (expect != get)))
%!   error ("didn't get what was expected.");
%! endif

To make the process easier, use the assert function. For example, with assert the previous test is reduced to:

%! a = [1, 2, 3; 4, 5, 6]; b = [1; 2];
%! assert (kron (b, a), [ a; 2*a ]);

assert can accept a tolerance so that you can compare results absolutely or relatively. For example, the following all succeed:

%!test assert (1+eps, 1, 2*eps)          # absolute error
%!test assert (100+100*eps, 100, -2*eps) # relative error

You can also do the comparison yourself, but still have assert generate the error:

%!test assert (isempty ([]))
%!test assert ([1, 2; 3, 4] > 0)

Because assert is so frequently used alone in a test block, there is a shorthand form:

%!assert (…)

which is equivalent to:

%!test assert (…)

Occasionally a block of tests will depend on having optional functionality in Octave. Before testing such blocks the availability of the required functionality must be checked. A %!testif HAVE_XXX block will only be run if Octave was compiled with functionality ‘HAVE_XXX’. For example, the sparse single value decomposition, svds(), depends on having the ARPACK library. All of the tests for svds begin with

%!testif HAVE_ARPACK

Review config.h or octave_config_info ("features") to see some of the possible values to check.

Sometimes during development there is a test that should work but is known to fail. You still want to leave the test in because when the final code is ready the test should pass, but you may not be able to fix it immediately. To avoid unnecessary bug reports for these known failures, mark the block with xtest rather than test:

%!xtest assert (1==0)
%!xtest fail ("success=1", "error")

In this case, the test will run and any failure will be reported. However, testing is not aborted and subsequent test blocks will be processed normally. Another use of xtest is for statistical tests which should pass most of the time but are known to fail occasionally.

Each block is evaluated in its own function environment, which means that variables defined in one block are not automatically shared with other blocks. If you do want to share variables, then you must declare them as shared before you use them. For example, the following declares the variable a, gives it an initial value (default is empty), and then uses it in several subsequent tests.

%!shared a
%! a = [1, 2, 3; 4, 5, 6];
%!assert (kron ([1; 2], a), [ a; 2*a ]);
%!assert (kron ([1, 2], a), [ a, 2*a ]);
%!assert (kron ([1,2; 3,4], a), [ a,2*a; 3*a,4*a ]);

You can share several variables at the same time:

%!shared a, b

You can also share test functions:

%!function a = fn (b)
%!  a = 2*b;
%!assert (fn(2), 4);

Note that all previous variables and values are lost when a new shared block is declared.

Remember that %!function begins a new block and that %!endfunction ends this block. Be aware that until a new block is started, lines starting with ‘%!<space>’ will be discarded as comments. The following is nearly identical to the example above, but does nothing.

%!function a = fn (b)
%!  a = 2*b;
%! assert (fn(2), 4);

Because there is a space after ‘%!’ the assert statement does not begin a new block and this line is treated as a comment.

Error and warning blocks are like test blocks, but they only succeed if the code generates an error. You can check the text of the error is correct using an optional regular expression <pattern>. For example:

%!error <passes!> error ("this test passes!");

If the code doesn’t generate an error, the test fails. For example:

%!error "this is an error because it succeeds.";


  ***** error "this is an error because it succeeds.";
!!!!! test failed: no error

It is important to automate the tests as much as possible, however some tests require user interaction. These can be isolated into demo blocks, which if you are in batch mode, are only run when called with demo or the verbose option to test. The code is displayed before it is executed. For example,

%! t = [0:0.01:2*pi]; x = sin (t);
%! plot (t, x);
%! # you should now see a sine wave in your figure window


funcname example 1:
 t = [0:0.01:2*pi]; x = sin (t);
 plot (t, x);
 # you should now see a sine wave in your figure window

Press <enter> to continue:

Note that demo blocks cannot use any shared variables. This is so that they can be executed by themselves, ignoring all other tests.

If you want to temporarily disable a test block, put # in place of the block type. This creates a comment block which is echoed in the log file but not executed. For example:

%! t = [0:0.01:2*pi]; x = sin (t);
%! plot (t, x);
%! # you should now see a sine wave in your figure window

The following trivial code snippet provides examples for the use of fail, assert, error and xtest:

function output = must_be_zero (input)
  if (input != 0)
    error ("Nonzero input!")
  output = input;

%!fail ("must_be_zero (1)");
%!assert (must_be_zero (0), 0);
%!error <Nonzero> must_be_zero (1);
%!xtest error ("This code generates an error");

When putting this a file must_be_zero.m, and running the test, we see

test must_be_zero verbose

>>>>> /path/to/must_be_zero.m
  ***** fail ("must_be_zero (1)");
  ***** assert (must_be_zero (0), 0);
  ***** error <Nonzero> must_be_zero (1);
  ***** xtest error ("This code generates an error");
!!!!! known failure
This code generates an error
PASSES 4 out of 4 tests (1 expected failures)

Block type summary:


check that entire block is correct

%!testif HAVE_XXX

check block only if Octave was compiled with feature HAVE_XXX.


check block, report a test failure but do not abort testing.


check for correct error message


check for correct warning message


demo only executes in interactive mode


comment: ignore everything within the block

%!shared x,y,z

declare variables for use in multiple tests


define a function for use in multiple tests


close a function definition

%!assert (x, y, tol)

shorthand for %!test assert (x, y, tol)

You can also create test scripts for built-in functions and your own C++ functions. To do so, put a file with the bare function name (no .m extension) in a directory in the load path and it will be discovered by the test function. Alternatively, you can embed tests directly in your C++ code:

%!test disp ("this is a test")


#if 0
%!test disp ("this is a test")

However, in this case the raw source code will need to be on the load path and the user will have to remember to type test ("").

Function File: assert (cond)
Function File: assert (cond, errmsg)
Function File: assert (cond, errmsg, …)
Function File: assert (cond, msg_id, errmsg, …)
Function File: assert (observed, expected)
Function File: assert (observed, expected, tol)

Produce an error if the specified condition is not met.

assert can be called in three different ways.

assert (cond)
assert (cond, errmsg)
assert (cond, errmsg, …)
assert (cond, msg_id, errmsg, …)

Called with a single argument cond, assert produces an error if cond is false (numeric zero).

Any additional arguments are passed to the error function for processing.

assert (observed, expected)

Produce an error if observed is not the same as expected.

Note that observed and expected can be scalars, vectors, matrices, strings, cell arrays, or structures.

assert (observed, expected, tol)

Produce an error if observed is not the same as expected but equality comparison for numeric data uses a tolerance tol.

If tol is positive then it is an absolute tolerance which will produce an error if abs (observed - expected) > abs (tol).

If tol is negative then it is a relative tolerance which will produce an error if abs (observed - expected) > abs (tol * expected).

If expected is zero tol will always be interpreted as an absolute tolerance.

If tol is not scalar its dimensions must agree with those of observed and expected and tests are performed on an element-by-element basis.

See also: fail, test, error, isequal.

Function File: fail (code)
Function File: fail (code, pattern)
Function File: fail (code, "warning")
Function File: fail (code, "warning", pattern)

Return true if code fails with an error message matching pattern, otherwise produce an error.

code must be in the form of a string that is passed to the Octave interpreter via the evalin function, i.e., a (quoted) string constant or a string variable.

Note that if code runs successfully, rather than failing, the error printed is:

          expected error <.> but got none

If called with two arguments, the return value will be true only if code fails with an error message containing pattern (case sensitive). If the code fails with a different error than the one specified in pattern then the message produced is:

          expected <pattern>
          but got <text of actual error>

The angle brackets are not part of the output.

When called with the "warning" option fail will produce an error if executing the code produces no warning.

See also: assert, error.

Next: , Up: Test and Demo Functions   [Contents][Index]