Skip to content

Added -throwerrors option to runxunit #27

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/TestCase.m
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
self.Location = which(class(self));
end

function did_pass = run(self, monitor)
function did_pass = run(self, monitor, throw)
%run Execute the test case
% test_case.run(monitor) calls the TestCase object's setUp()
% method, then the test method, then the tearDown() method.
Expand All @@ -53,37 +53,60 @@
% TestRunMonitor object. Typically it is either a TestRunLogger
% subclass or a CommandWindowTestRunDisplay subclass.
%
% test_case.run(monitor, throw) the additional argument
% throw is a trul/false flag indicating whether to throw
% errors rather than merely log them. Default is false if
% not supplied.
%
% test_case.run() automatically uses a
% CommandWindowTestRunDisplay object in order to print test
% suite execution information to the Command Window.
%


if nargin < 2
monitor = CommandWindowTestRunDisplay();
end

if nargin < 3
throw = false;
end

did_pass = true;
monitor.testComponentStarted(self);

try
if throw
% user wants errors from the test case to actually be
% thrown and stop program execution so they can be debugged
self.setUp();
f = str2func(self.MethodName);

f(self);
self.tearDown();
did_pass = true;
else
% normal test behaviour, errors are merely logged
try
% Call the test method.
f(self);
catch failureException
monitor.testCaseFailure(self, failureException);
self.setUp();
f = str2func(self.MethodName);

try
% Call the test method.
f(self);
catch failureException
monitor.testCaseFailure(self, failureException);
did_pass = false;
end

self.tearDown();

catch errorException
monitor.testCaseError(self, errorException);
did_pass = false;
end

self.tearDown();

catch errorException
monitor.testCaseError(self, errorException);
did_pass = false;
end

monitor.testComponentFinished(self, did_pass);

end

function num = numTestCases(self)
Expand Down
2 changes: 1 addition & 1 deletion src/TestComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

%run Execute test cases
% obj.run() executes all the test cases in the test component
did_pass_out = run(self, monitor)
did_pass_out = run(self, monitor, throw)

%numTestCases Number of test cases in test component
num = numTestCases(self)
Expand Down
8 changes: 6 additions & 2 deletions src/TestSuite.m
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
end
end

function did_pass_out = run(self, monitor)
function did_pass_out = run(self, monitor, throw)
%run Execute test cases in test suite
% did_pass = suite.run() executes all test cases in the test
% suite, returning a logical value indicating whether or not all
Expand All @@ -76,13 +76,17 @@
monitor = CommandWindowTestRunDisplay();
end

if nargin < 3
throw = false;
end

monitor.testComponentStarted(self);
did_pass = true;

self.setUp();

for k = 1:numel(self.TestComponents)
this_component_passed = self.TestComponents{k}.run(monitor);
this_component_passed = self.TestComponents{k}.run(monitor, throw);
did_pass = did_pass && this_component_passed;
end

Expand Down
14 changes: 11 additions & 3 deletions src/runxunit.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@
% Window. This format is compatible with JUnit, and can be read by many
% tools.
%
% runxunit(..., '-throwerrors') causes runxunit to throw errors when
% encountered rather than simply reporting them. Can be useful for
% debugging tests, or using tests for development, as well as a
% post-feature development tool.
%
% out = runxunit(...) returns a logical value that is true if all the
% tests passed.
%
Expand Down Expand Up @@ -89,7 +94,7 @@
if nargin < 1
suite = TestSuite.fromPwd();
else
[name_list, verbose, logfile, isxml] = getInputNames(varargin{:});
[name_list, verbose, logfile, isxml, throw] = getInputNames(varargin{:});
if numel(name_list) == 0
suite = TestSuite.fromPwd();
elseif numel(name_list) == 1
Expand Down Expand Up @@ -133,17 +138,18 @@
else
monitor = TestRunDisplay(logfile_handle);
end
did_pass = suite.run(monitor);
did_pass = suite.run(monitor, throw);

if nargout > 0
out = did_pass;
end

function [name_list, verbose, logfile, isxml] = getInputNames(varargin)
function [name_list, verbose, logfile, isxml, throw] = getInputNames(varargin)
name_list = {};
verbose = false;
logfile = '';
isxml = false;
throw = false;
k = 1;
while k <= numel(varargin)
arg = varargin{k};
Expand All @@ -169,6 +175,8 @@
logfile = varargin{k+1};
k = k + 1;
end
elseif strcmp(arg, '-throwerrors')
throw = true;
else
warning('runxunit:unrecognizedOption', 'Unrecognized option: %s', arg);
end
Expand Down