uniform unittest framework for GNU Octave and Matlab
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

194 lines
4.0 KiB

function ret = mUnittest(script)
%mUnittest - uniform unittest framework for GNU Octave and Matlab
% the created .xml Reports are designed to work with Jenkins JUnit publisher
% The MIT License (MIT) https://github.com/markuman/mUnittest
if ischar(script)
% set 'script' as testName
className(script);
fprintf('\n\n mUnittest: %s \n\n', className())
% start time
tic
try
% call testscript
eval(script);
ret = 0;
catch
ret = 1;
end
% print summary
summary();
else
ret = 1;
end%if
end%function mUnittest
function summary()
% get number of passed and failed tests
p = passed();
f = failed();
% report numbers
fprintf('\n\n PASSED %d OF %d \n\n', p, p + f);
% write report
fid = fopen(sprintf('%sReport.xml', className()), 'w');
num = testCount();
xml = globeString();
t = Time();
fprintf(fid, '<testsuite tests="%d" time="%.f">%s\n</testsuite>',num, t, xml);
fclose(fid);
% reset testCounts
testCount(-1);
passed(-1);
failed(-1);
globeString();
end
% overloaded assert
function assert(cond, msg, errormsg)
t = Time();
t = toc - t;
testCount();
num = testCount();
if nargin <= 1
msg = sprintf('%s_%d', className(), num);
end
if nargin <= 2
errormsg = sprintf('Test %d failed', num);
end
if cond
passed(msg);
globeString(sprintf('\n\t<testcase classname="%s" name="%s" time="%.2f" />', className(), msg, t));
else
failed(msg);
globeString(sprintf('\n\t<testcase classname="%s" name="%s" time="%.2f" >\n\t\t<failure type="assert"> %s </failure>\n\t</testcase>', className(), msg, t, errormsg));
end
Time(t);
end%function
function ret = Time(t)
persistent testTime
num = testCount();
if num == 0
testTime = 0;
end
if nargin == 1
testTime = testTime + t;
elseif nargout == 1
ret = testTime;
end%if
end%function testTime
function ret = testCount(varargin)
persistent countTests
if isempty(countTests)
countTests = 0;
end
% when there is one nargout, return current value of countTests
if nargout == 1
ret = countTests;
elseif nargin == 0
% when there is no nargin, increase value of countTests
countTests = countTests + 1;
ret = true;
elseif nargin == 1
% when there is one nargin, clear countTests
clear countTests
end%if
end%function testCount
function ret = passed(varargin)
persistent countPassed
if isempty(countPassed)
countPassed = 0;
end
if nargout == 1
ret = countPassed;
elseif nargin == 1 && isnumeric(varargin{1})
clear countPassed
else
countPassed = countPassed + 1;
ret = true;
num = testCount();
fprintf('\n%d \t passed \t %s', num, varargin{1})
end
end%function failed
function ret = failed(varargin)
persistent countFailed
if isempty(countFailed)
countFailed = 0;
end
if nargout == 1
ret = countFailed;
elseif nargin == 1 && isnumeric(varargin{1})
clear countFailed
else
countFailed = countFailed + 1;
ret = true;
num = testCount();
fprintf('\n%d \t failed \t %s', num, varargin{1})
end
end%function failed
function ret = className(name)
persistent n
if nargin == 1
n = name;
ret = true;
elseif nargout == 1
ret = n;
else
ret = false;
end
end%function testName
function ret = globeString(in)
persistent xml
if isempty(xml)
xml = '';
end
if nargin == 1
xml = [xml in];
end
if nargout == 1
ret = xml;
end
if testCount() == 0
clear xml
end
end