Code covered by the BSD License  

Highlights from
Automatic validation of software

  • prtest1 For demonstration of the VALIDATE function. No intrinsic value!
  • prtest2 For demonstration of the VALIDATE function. No intrinsic value!
  • prtest3 For demonstration of the VALIDATE function. No intrinsic value!
  • validate(dirname) VALIDATE Validate software using standard test files.
  • View all files
from Automatic validation of software by Toby Driscoll
Validate software using a test library of functions in a directory.

validate(dirname)
function failfun = validate(dirname)
% VALIDATE Validate software using standard test files.
%   VALIDATE DIRNAME runs each M-file in the directory DIRNAME. Each M-file
%   should be a function that takes no inputs and returns a logical vector
%   or scalar. Three outcomes are possible: 
%
%     'passed'    All return values are true.  
%     'failed'    Some return value is false.
%     'crashed'   Function threw an error.
%
%   A report is generated dynamically in the command window.
%
%   VALIDATE by itself tries to find a directory named 'validtests' in the
%   directory in which validate.m resides.
%
%   FAILED = VALIDATE('DIRNAME') returns a cell array of all functions that
%   either failed or crashed.
%
%   Be advised that all figures will be closed prior to testing. Also,
%   since the tests are run in the current workspace, you might want to
%   start a new copy of matlab before running VALIDATE, so that the results
%   are reproducible.
%
%   See also TRY.

% Copyright 2008 by Toby Driscoll.

if nargin < 1
  % Attempt to find "validtests" directory.
  w = which('validate.m');
  dirname = fileparts(w);
  dirname = fullfile(dirname,'validtests');
end
  
if exist(dirname)~=7
  msg = ['The name "' dirname '" does not appear to be a directory on the path.'];
  error('validate:nodir',msg)
end

dirlist = dir( fullfile(dirname,'*.m') );
mfile = {dirlist.name};

fprintf('\nTesting %i functions...\n\n',length(mfile))
failed = zeros(length(mfile),1);

addpath(dirname)
warnstate = warning;
warning off
for j = 1:length(mfile)
  
  fun = mfile{j}(1:end-2);
  link = ['<a href="matlab: edit ' dirname filesep fun '">' fun '</a>'];
  msg = ['  Function #' num2str(j) ' (' link ')... ' ];
  msg = strrep(msg,'\','\\');  % escape \ for fprintf
  fprintf(msg);
  
  try
    close all
    failed(j) = ~ all(feval( fun ));
    if failed(j)
      fprintf('FAILED\n')
    else
      fprintf('passed\n')
      pause(0.1)
      %fprintf( repmat('\b',1,numchar) )
    end
  catch
    failed(j) = -1;
    fprintf('CRASHED: ')
    msg = lasterror;  
    lf = findstr(sprintf('\n'),msg.message); 
    if ~isempty(lf), msg.message(1:lf(end))=[]; end
    fprintf([msg.message '\n'])
  end
  
end
rmpath(dirname)
warning(warnstate)

if all(~failed)
  fprintf('\nAll tests passed!\n\n')
  if nargout>0, failfun = {}; end
else
  fprintf('\n%i failed and %i crashed\n\n',sum(failed>0),sum(failed<0))
  failfun = mfile(failed~=0);
end

end

Contact us at files@mathworks.com