No BSD License  

Highlights from
MUnit: a unit testing framework in Matlab

MUnit: a unit testing framework in Matlab

by

 

05 Jun 2006 (Updated )

A complete unit testing framework in Matlab

testrunnerTextSimple(tu,calls)
function tuo = testrunnerTextSimple(tu,calls)
% TESTRUNNRETEXTSIMPLE  Adds callbacks to a testunit to allow its running
%
%  This function adds to a testunit the callbacks that generates the output
% of the testing process, in this case the output is an ascii stream on the
% console simplified.
%
%  Params
%  ------
% IN:
%  tu       = The testunit or testset to modify or execute.
%  calls    = Must modify or execute? (def=false=execute)

% Check params:
if nargin<1 error('A testset or testunit is required!'); end
if nargin<2 calls = false; end

% Check the operation type:
if calls
    % Check the type of testunit:
    switch class(tu)
        case 'testunit'
            % Adding only the done callback:
            tu = addCallback(tu,@TUDispResults,2,true);
        case 'testset'
            % Adding reset and done local callbacks:
            tu = addCallback(tu,@TSDispHead,1,true);
            tu = addCallback(tu,@TSDispResults,2,true);
            
            % I've to "runnerize" recursively the sub-tests:
            tests = get(tu,'tests');
            for ind=1:size(tests,2)
                tests{ind} = testrunnerTextSimple(tests{ind},calls);
            end
            % Saving:
            tu = set(tu,'tests',tests);
        otherwise
            error('Only testunit and testset classes are supported!');
    end
else
    % Run the test:
    disp(['==> Testing process starts at ',datestr(now)]);
    tu = test(tu);
    disp(['==> Testing process ends at ',datestr(now)]);
end

% Returning:
tuo = tu;

% ------------------------ LOCAL FUNCTIONS ------------------------

% Function that displays the result of a test:
function tuo = TUDispResults(tu,event)

% Display the test name and the status:
if get(tu,'status')
    % Displaing the result:
    fprintf(1,'.');
else
    % Displaing the result:
    fprintf(1,'F\n');
    disp(sprintf('*** %7s: FAILURE! "%s"', get(tu,'name'), get(tu,'desc')));
    
    % Check the error:
    err = get(tu,'err');
    if not(strcmp(err,''))
        % Display the error:
        disp(sprintf('%10s\tlasterr: %s','',err));
    end
end
tuo = tu;

% -----------------------------------------------------------------

% Function that displays the heading of a testset:
function tso = TSDispHead(ts,event)

% Display the test name and the status:
disp(get(ts,'name'));
tso = ts;

% -----------------------------------------------------------------

% Function that displays the result of a test:
function tso = TSDispResults(ts,event)

% Display the test name and the status:
if get(ts,'status')
    result = 'All tests SUCCESSES';
else
    result = 'Some tests FAILS';
end
stats = get(ts,'stats');
disp(sprintf('\n%s in %.3f seconds!',result,stats.elapsed));
tso = ts;

Contact us