little rewrite of mUnittest

master
Markus Bergholz 8 years ago
parent 82c74555ba
commit 0dd6f54abc
  1. 205
      mUnittest.m
  2. 5
      testscript.m

@ -30,8 +30,13 @@ end%function mUnittest
function summary()
% get number of passed and failed tests
p = passed();
f = failed();
result = assert('this', 'will', 'end', 'here', ':-)');
f = result.countFailed;
p = result.countPassed;
num = result.countTests;
t = result.t;
xml = result.xml;
% report numbers
fprintf('\n\n PASSED %d OF %d \n\n', p, p + f);
@ -39,124 +44,89 @@ function summary()
% 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 = assert(cond, msg, errormsg, successmsg, varargin)
persistent t
persistent countTests
persistent countFailed
persistent countPassed
persistent countBlocks
persistent xml
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
if (nargin == 5)
% return results
ret.t = t;
ret.countTests = countTests;
ret.countFailed = countFailed;
ret.countPassed = countPassed;
ret.countBocks = countBlocks;
ret.xml = xml;
% reset everything
builtin('clear', 't');
builtin('clear', 'countTests');
builtin('clear', 'countFailed');
builtin('clear', 'countPassed');
builtin('clear', 'countBlocks');
builtin('clear', 'xml');
else
if isempty(t), t = 0; end
if isempty(countTests), countTests = 0; end
if isempty(countFailed), countFailed = 0; end
if isempty(countPassed), countPassed = 0; end
if isempty(countBlocks), countBlocks = 0; end
if isempty(xml), xml = ''; end
end%function testTime
this_time = toc - t;
function ret = testCount(varargin)
% increase number of sings tests (asserts)
countTests = countTests + 1;
persistent countTests
if isempty(countTests)
countTests = 0;
end
% set passed and failed messages
if nargin <= 1
msg = sprintf('%s_%d', className(), countTests);
end
if nargin <= 2
errormsg = sprintf('Test %d failed', countTests);
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)
if cond
%% PASSED
% increase number of passed tests
countPassed = countPassed + 1;
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
% verbose output
fprintf('\n%d \t passed \t %s', countTests, msg)
end%function failed
% concatinate global string for xml output
xml = [xml sprintf('\n\t<testcase classname="%s #%d " name="%s" time="%.2f" />', className(), protectCount() , msg, this_time)];
else
%% FAILED
% increase number of failed tests
countFailed = countFailed + 1;
function ret = failed(varargin)
% verbose output
fprintf('\n%d \t failed \t %s', countTests, errormsg)
persistent countFailed
if isempty(countFailed)
countFailed = 0;
end
% concatinate global string for xml output
xml = [xml sprintf('\n\t<testcase classname="%s #%d " name="%s" time="%.2f" >\n\t\t<failure type="assert"> %s </failure>\n\t</testcase>', className(), protectCount(), msg, t, errormsg)];
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
t = toc;
end
end%function failed
end%function assert
function ret = className(name)
@ -170,25 +140,28 @@ function ret = className(name)
ret = false;
end
end%function testName
end%function className
function ret = globeString(in)
function clear()
persistent xml
if isempty(xml)
xml = '';
end
if nargin == 1
xml = [xml in];
end
protectCount(1);
builtin('clear');
if nargout == 1
ret = xml;
end
end%function
function ret = protectCount(in)
persistent n
if isempty(n), n = 0; end
if testCount() == 0
clear xml
if nargin == 1
n = n + in;
elseif nargout == 1
ret = n;
elseif nargin == 5
builtin('clear', 'n');
else
ret = n;
end
end
end%function

@ -1,8 +1,13 @@
%% block 1
clear
assert(4 == 4, 'short test description');
assert(sum(1:10) == 55, 'summenprüfung');
assert(randi([0 2]) == 1, 'could fail', 'zweifelhafte implementation');
pause(3)
assert(0 == 1);
%% test 2
clear
assert( 1 == 1, 'short description of test')
pause(2)
assert( 1 == 0, 'short description of test', 'optionale error message when it failed')

Loading…
Cancel
Save