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

StestTestSet02(in)
% This is a simple testset collectable!
function out = StestTestSet02(in)
out = str2func(in);

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

function tso = MUnitSetUp(ts,event)
% Generating a set of usefull matrices to be used in tests

% Setting up a non singular matrix:
disp('Setting up...');
m1 = [1,2,3;4,5,6;7,8,9];
m2 = [1,1,1;0,1,1;0,0,1];

% Returning the testset with the new env:
tso = set(ts,'env',{m1,m2});

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

% This is the shoutdown function, this is optional:
function tso = MUnitShoutDown(ts,event)
% Do notting... but test!

% Code that generates the new ts:
disp('Shoutting down...');
tso = ts;

% =================================================================

function [tu,status,tdata] = MUnitTest_Inv1(tu,tdata)
% Check the function 'inv'

% Get the env:
env=tdata{1}; m1=env{1};

% Init the status:
status = true;

% A list of asserts:
try
    % Check the existence of the inv:
    try m1_1=inv(m1);
    catch failure(tu,'Cannot invert the matrix m1!'); end
    % Check if m1_1 is the inverse matrix:
    assertTrue(tu, ...
        sum(sum(eye(size(m1))==m1*m1_1))==9, ...
        'The inverse of the non-singular matrix m1 isn''t really the inverse!');
catch
    % Bad!!!
    status = false;
end

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

function [tu,status,tdata] = MUnitTest_Inv2(tu,tdata)
% Check the function 'inv' with another matrix

% Get the env:
env=tdata{1}; m2=env{2};

% Init the status:
status = true;

% A list of asserts:
try
    % Check the existence of the inv:
    try m2_1=inv(m2);
    catch failure(tu,'Cannot invert the matrix m2!'); end
    % Check if m2_1 is the inverse matrix:
    assertTrue(tu, ...
        sum(sum(eye(size(m2))==m2*m2_1))==9, ...
        'The inverse of the non-singular matrix m2 isn''t really the inverse!');
catch
    % Bad!!!
    status = false;
end

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

Contact us