From 1c74ac68b09bf15ed0beec7900f8fbcd865be467 Mon Sep 17 00:00:00 2001 From: Richard Crozier Date: Mon, 25 Feb 2019 12:31:28 +0000 Subject: [PATCH] Added -throwerrors option to runxunit --- src/TestCase.m | 49 +++++++++++++++++++++++++++++++++------------ src/TestComponent.m | 2 +- src/TestSuite.m | 8 ++++++-- src/runxunit.m | 14 ++++++++++--- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/src/TestCase.m b/src/TestCase.m index b64ce83..a0decc7 100644 --- a/src/TestCase.m +++ b/src/TestCase.m @@ -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. @@ -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) diff --git a/src/TestComponent.m b/src/TestComponent.m index d979962..bc5b929 100644 --- a/src/TestComponent.m +++ b/src/TestComponent.m @@ -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) diff --git a/src/TestSuite.m b/src/TestSuite.m index d8ca59b..e1177d8 100644 --- a/src/TestSuite.m +++ b/src/TestSuite.m @@ -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 @@ -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 diff --git a/src/runxunit.m b/src/runxunit.m index 60018f3..a8efb05 100644 --- a/src/runxunit.m +++ b/src/runxunit.m @@ -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. % @@ -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 @@ -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}; @@ -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