master
Markus Bergholz 8 years ago
parent b424efb91d
commit 04ddf14963
  1. 8
      README.md
  2. 3
      mUnittest.m
  3. 225
      mutest.m
  4. 51
      mutesttest.m

@ -1,2 +1,8 @@
# mutest
uniform unittest framework for GNU Octave and Matlab
* uniform unittest framework for GNU Octave and Matlab
* `mUnittest('testscript')` will create `testscriptReport.xml` which is designed to work with Jenkins JUnit publish plugin.
* `mUnittest()` is compatible with `runtests()` of mathworks.

@ -1,4 +1,7 @@
function ret = mUnittest(script)
%mutest - simply matlab unittest framework
% designed to have one uniform framework for GNU Octave and Matlab
% the created .xml Reports are designed to work with Jenkins JUnit publisher
if ischar(script)

@ -1,225 +0,0 @@
classdef mutest
%mutest - simply matlab unittest framework
% designed to have one uniform framework for GNU Octave and Matlab
%
% r = mutest(); %initialize
%
% testing with r.compare(this, that);
% or with a description r.compare(this,that, 'short test description');
% r = r.compare(4,4);
% r = r.compare(1,[]);
% r = r.compare(sum(1:10), 55, 'check calculation');
% r = r.compare(rand(1), 1, 'will fail');
%
% get summary
% r.summary();
%
% access number of failed/passed
% [r, passed, failed] = r.summary();
properties(Access=protected)
passed = 0;
failed = 0;
testnr = 0;
sum_f = '';
sum_t = 0;
end
properties
classname = 'mutest';
xml_u = '';
end
methods(Access=protected)
function obj = verbose(obj, testName)
% write head of the test
if obj.testnr <= 1
fprintf(['\nTestnr\tDescription\t\t\tResult\n' ...
'==============================================\n'])
end
if length(testName) == 0
obj.xml_u = [obj.xml_u sprintf('\n\t<testcase classname="%s" name="mutext_%d"', obj.classname, obj.testnr)];
testName = sprintf('...\t\t\t\t');
else
obj.xml_u = [obj.xml_u sprintf('\n\t<testcase classname="%s" name="%s"', obj.classname, testName)];
if length(testName) < 8
testName = sprintf('%s\t\t\t\t', testName);
elseif length(testName) < 16
testName = sprintf('%s\t\t\t', testName);
elseif length(testName) < 28
testName = sprintf('%s\t\t', testName);
else
testName = sprintf('%s..\t', testName(1:28));
end
end
fprintf('%d\t%s', obj.testnr, testName)
end%function verbose
end%methods protected
methods
function obj = mutest(classname)
tic
if nargin == 1
obj.classname = 'mutest';
elseif nargin == 2
obj.classname = classname;
end
end
function obj = exception(obj, testName, explanation)
if nargin <= 1
testName = '';
end
if nargin <= 2
explanation = 'exception';
end
% incr testnr
obj.testnr = obj.testnr + 1;
% verbose output
obj = obj.verbose(testName);
obj.failed = obj.failed + 1;
obj.sum_f = [obj.sum_f sprintf('%d\tfailed\n', obj.testnr)];
fprintf('failed\n')
t = toc - obj.sum_t;
obj.xml_u = [obj.xml_u sprintf(' time="%.2f">\n\t\t<failure type="exception"> %s </failure>\n\t</testcase>', t, explanation)];
obj.sum_t = obj.sum_t + t;
end%function exception
function obj = noException(obj, testName)
if nargin <= 1
testName = '';
end
% incr testnr
obj.testnr = obj.testnr + 1;
% verbose output
obj = obj.verbose(testName);
obj.passed = obj.passed + 1;
fprintf('passed\n')
t = toc - obj.sum_t;
obj.xml_u = [obj.xml_u sprintf(' time="%.2f"/>', t)];
obj.sum_t = obj.sum_t + t;
end%function exception
function obj = not(obj, A, B, testName, explanation)
if nargin <= 3
testName = '';
end
if nargin <= 4
explanation = 'It''is equal but it shouldn''t be equal';
end
% incr testnr
obj.testnr = obj.testnr + 1;
% verbose output
obj = obj.verbose(testName);
try
assert(A ~= B);
obj.passed = obj.passed + 1;
fprintf('passed\n')
t = toc - obj.sum_t;
obj.xml_u = [obj.xml_u sprintf(' time="%.2f"/>', t)];
catch
t = toc - obj.sum_t;
obj.failed = obj.failed + 1;
obj.sum_f = [obj.sum_f sprintf('%d\tfailed\n', obj.testnr)];
fprintf('failed\n')
obj.xml_u = [obj.xml_u sprintf(' time="%.2f">\n\t\t<failure type="Equal"> %s </failure>\n\t</testcase>', t, explanation)];
end%try
obj.sum_t = obj.sum_t + t;
end%function not
function obj = compare(obj, A, B, testName, explanation)
if nargin <= 3
testName = '';
end
if nargin <= 4
explanation = 'It''s not equal but it should be equal';
end
% incr testnr
obj.testnr = obj.testnr + 1;
% verbose output
obj = obj.verbose(testName);
try
assert(A ~= B);
obj.passed = obj.passed + 1;
fprintf('passed\n')
t = toc - obj.sum_t;
obj.xml_u = [obj.xml_u sprintf(' time="%.2f"/>', t)];
catch
t = toc - obj.sum_t;
obj.failed = obj.failed + 1;
obj.sum_f = [obj.sum_f sprintf('%d\tfailed\n', obj.testnr)];
fprintf('failed\n')
obj.xml_u = [obj.xml_u sprintf(' time="%.2f">\n\t\t<failure type="Equal"> %s </failure>\n\t</testcase>', t, explanation)];
end%try
obj.sum_t = obj.sum_t + t;
end%function comparing
function [obj, passed, failed] = summary(obj)
if nargout == 0
fprintf('\n\nmutest Summary:\n')
fprintf('\t%d/%d passed\n', obj.passed, obj.passed + obj.failed)
fprintf('\t%.4f seconds testing time.\n\n', obj.sum_t)
if obj.failed > 0
fprintf('Testnr Status\n==============\n')
fprintf([obj.sum_f '\n\n'])
end
else
failed = obj.failed;
passed = obj.passed;
end%
end%function
% write xUnit report
function writereport(obj, name)
if nargin == 1
name = 'testreport.xml';
elseif nargin == 2
[~,~,ext] = fileparts(name);
if ~strcmp(ext, '.xml')
name = [name '.xml'];
end
end
fid = fopen(name, 'w');
obj.xml_u = sprintf('<testsuite tests="%d" time="%.2f">%s\n</testsuite>', obj.failed + obj.passed, obj.sum_t, obj.xml_u );
fprintf(fid, '%s', obj.xml_u);
fclose(fid);
end%
end%methods
end%classdef

@ -1,51 +0,0 @@
r = mutest('mutest beispiel');
r = r.compare(4,4, 'short test description');
r = r.compare(sum(1:10), 55, 'summenprüfung');
r = r.compare(randi([0 2]), 1, 'could fail');
r = r.compare(0, 1);
pause(3)
r = r.compare(0, 1, 'with name');
r = r.compare(0, 0, 'with name', 'and explanation');
r = r.compare(5,5);
pause(1)
r = r.not(0, 1, 'this should not be that because of');
r = r.not(0, 0);
r = r.not(0, 0, 'with name');
pause(1)
r = r.not(0, 0, 'with name', 'and explanation');
try
some wired stuff
r = r.noException();
catch
r = r.exception();
end
try
some wired stuff
r = r.noException();
catch
r = r.exception('with name');
end
try
some wired stuff
r = r.noException();
catch
r = r.exception('with name', 'and explanation');
end
try
rand(1);
r = r.noException();
catch
r = r.exception();
end
try
rand(1);
r = r.noException('no surprise here');
catch
r = r.exception();
end
r.summary();
r.writereport();
[r, passed, failed] = r.summary();
% quit(failed > 0)
Loading…
Cancel
Save