|
| 1 | +function tc_summary = run_tcase(tcase, mode, logfile, test_list) |
| 2 | +% run_tcase -- Run a MUnit test case and print the test results. |
| 3 | +% |
| 4 | +%****f* Drivers/run_tcase |
| 5 | +% |
| 6 | +% NAME |
| 7 | +% run_tcase -- Run a MUnit test case and print the test results. |
| 8 | +% |
| 9 | +% SYNOPSIS |
| 10 | +% tc_summary = run_tcase(tcase, [mode], [logfile], [test_list]) |
| 11 | +% |
| 12 | +% INPUTS |
| 13 | +% * tcase -- test case (string, function handle, or tcase_s). |
| 14 | +% * mode -- (optional) level of detail for output diagnostics. |
| 15 | +% * logfile -- (optional) name of log file for writing output. |
| 16 | +% * test_list -- (optional) name(s) or ordinal number(s) of specific tests |
| 17 | +% to run. |
| 18 | +% (string or integer, or cell array of strings or integer). |
| 19 | +% |
| 20 | +% OUTPUTS |
| 21 | +% * tc_summary -- test case summary struct (tc_summary_s). |
| 22 | +% |
| 23 | +% SYNOPSIS |
| 24 | +% run_tcase(tcase) % default |
| 25 | +% run_tcase(tcase, mode) % specify level of detail mode. |
| 26 | +% run_tcase(tcase, '', logfile) % specify log file. |
| 27 | +% run_tcase(tcase, '', '', test_list) % specify tests to run. |
| 28 | +% |
| 29 | +% SIDE EFFECTS |
| 30 | +% |
| 31 | +% |
| 32 | +% DESCRIPTION |
| 33 | +% run_tcase is the main driver for running a test case. |
| 34 | +% |
| 35 | +% tcase is one of the following: |
| 36 | +% * string containing name of test case (i.e. file/function) to run. |
| 37 | +% * function handle of the test case to run (@tcase). |
| 38 | +% * a test case struct (tcase_s). |
| 39 | +% |
| 40 | +% The mode argument controls level of detail of output as follows: |
| 41 | +% * 'silent' -- no output. |
| 42 | +% * 'minimal' -- print test case summary. |
| 43 | +% * 'normal' -- print names of failed tests and test case summary. |
| 44 | +% * 'verbose' -- print all test names and their status, and test case summary. |
| 45 | +% * 'details' -- print diagnostics while running tests as well all test names |
| 46 | +% and their status, and test case summary. |
| 47 | +% |
| 48 | +% If logfile is specified, output is written to logfile. |
| 49 | +% Note that info printed by tested functions is still displayed to command window. |
| 50 | +% |
| 51 | +% The test_list argument allows the optional execution of specific test(s) |
| 52 | +% within test case. By default, all tests within a test case are run. |
| 53 | +% If test_list is a string or cell array of strings, then the tests with the |
| 54 | +% matching names are run. If test_list is a number or cell array of numbers, |
| 55 | +% then the tests with matching order in the test list are run, i.e. |
| 56 | +% if test_list = {1, 3}, then the first and third tests are run. If items |
| 57 | +% in test_list that do not match by test name or test order number are ignored. |
| 58 | +% |
| 59 | +% EXAMPLE |
| 60 | +% |
| 61 | +% |
| 62 | +% WARNINGS |
| 63 | +% |
| 64 | +% |
| 65 | +% ERRORS |
| 66 | +% MUNIT:UnknownTestCase |
| 67 | +% |
| 68 | +% NOTES |
| 69 | +% |
| 70 | +% |
| 71 | +% BUGS |
| 72 | +% |
| 73 | +% |
| 74 | +% TODO |
| 75 | +% |
| 76 | +% |
| 77 | +% ALGORITHM |
| 78 | +% |
| 79 | +% |
| 80 | +% REFERENCES |
| 81 | +% |
| 82 | +% |
| 83 | +% SEE ALSO |
| 84 | +% |
| 85 | +% |
| 86 | +% TOOLBOX |
| 87 | +% munit/munit |
| 88 | +% |
| 89 | +% CATEGORY |
| 90 | +% Application Drivers |
| 91 | +% |
| 92 | + |
| 93 | +% AUTHOR |
| 94 | +% Charlie Cornish |
| 95 | +% |
| 96 | +% CREATION DATE |
| 97 | +% 2004-Apr-28 |
| 98 | +% |
| 99 | +% COPYRIGHT |
| 100 | +% (c) 2004, 2005 Charles R. Cornish |
| 101 | +% |
| 102 | +% CREDITS |
| 103 | +% |
| 104 | +% |
| 105 | +% REVISION |
| 106 | +% $Revision: 112 $ |
| 107 | +% |
| 108 | +%*** |
| 109 | + |
| 110 | +% $Id: run_tcase.m 112 2005-09-13 05:53:51Z ccornish $ |
| 111 | + |
| 112 | +usage_str = ['Usage: [tc_summary] = ', mfilename, ... |
| 113 | + '(tcase, [mode], [logfile], [test_list])']; |
| 114 | + |
| 115 | +if (nargin < 1 || nargin > 4) |
| 116 | + error(usage_str); |
| 117 | +end |
| 118 | + |
| 119 | +tcname = ''; |
| 120 | +if (isa(tcase, 'function_handle')) |
| 121 | + tc = MU_tcase_create(tcase); |
| 122 | + tcname = func2str(tcase); |
| 123 | +% tc.pathstr = ''; |
| 124 | +elseif (isa(tcase, 'char')) |
| 125 | +% [pathstr, name, ext, versn] = fileparts(tcase); |
| 126 | +% curpath = pwd; |
| 127 | +% try |
| 128 | +% if (~isempty(pathstr)) |
| 129 | +% cd(pathstr); |
| 130 | +% tcname = name; |
| 131 | +% else |
| 132 | +% tcname = tcase; |
| 133 | +% end |
| 134 | +% tc = MU_tcase_create(tcname); |
| 135 | + tc = MU_tcase_create(tcase); |
| 136 | +% if (~isempty(pathstr)) |
| 137 | +% cd(curpath); |
| 138 | +% tc.pathstr = pathstr; |
| 139 | +% end |
| 140 | +% catch |
| 141 | +% err = lasterror; |
| 142 | +% cd(curpath); |
| 143 | +% rethrow(err); |
| 144 | +% end |
| 145 | +elseif (isa(tcase, 'struct')) |
| 146 | + tc = tcase; |
| 147 | + tcname = tc.name; |
| 148 | +else |
| 149 | + error('MUNIT:unknownTestCase', ... |
| 150 | + ['tcase is not a valid name, function handle, or struct', ... |
| 151 | + ' for a test case']); |
| 152 | +end |
| 153 | + |
| 154 | +if (~exist('mode', 'var') || isempty(mode)) |
| 155 | + mode = 'normal'; |
| 156 | +end |
| 157 | + |
| 158 | +if (exist('logfile', 'var') && ~isempty(logfile)) |
| 159 | + if (exist(logfile, 'file')) |
| 160 | + delete(logfile); |
| 161 | + end |
| 162 | + diary(logfile); |
| 163 | +end |
| 164 | + |
| 165 | +% Verify that tc is a tcase_s. |
| 166 | +if (~isfield(tc,'name') || ~isfield(tc,'tests')) |
| 167 | + if (ischar(tcase)) |
| 168 | + name = tcase; |
| 169 | + elseif (isa(tcase, 'function_handle')) |
| 170 | + name = func2str(tcase); |
| 171 | + else |
| 172 | + name = ''; |
| 173 | + end |
| 174 | + error('MUNIT:InvalidTestCase', ... |
| 175 | + ['tcase (', name, ') is not a valid test case structure.']); |
| 176 | +end |
| 177 | + |
| 178 | +%% Change direction to location of tcase file. |
| 179 | + |
| 180 | +%% Generate of list of tests to run. |
| 181 | +run_all_tests = 1; |
| 182 | +if (exist('test_list', 'var') && ~isempty(test_list)) |
| 183 | + run_all_tests = 0; |
| 184 | + if (ischar(test_list)) |
| 185 | + test_name_list = {test_list}; |
| 186 | + elseif (iscellstr(test_list)) |
| 187 | + test_name_list = test_list; |
| 188 | + elseif (isnumeric(test_list)) |
| 189 | + test_name_list = {}; |
| 190 | + ii = 0; |
| 191 | + for (i = 1:length(test_list)) |
| 192 | + test_num = test_list(i); |
| 193 | + if (test_num <= length(tc.tests)) |
| 194 | + ii = ii + 1; |
| 195 | + test_name_list{ii} = func2str(tc.tests{test_num}); |
| 196 | + else |
| 197 | + warning(['Specified test number (', num2str(test_num), ... |
| 198 | + ') is out of range of number of available tests', ... |
| 199 | + '(', num2str(length(tc.tests)), ')']); |
| 200 | + end |
| 201 | + end |
| 202 | + else |
| 203 | + error('MUNIT:invalidArgumentType', ... |
| 204 | + ['Argument (test_list) must be string or cell array of strings or ', ... |
| 205 | + 'a numeric scalar or array']); |
| 206 | + end |
| 207 | + if (isempty(test_name_list)) |
| 208 | + warning(['No matches found for specified tests.']); |
| 209 | + return |
| 210 | + end |
| 211 | +end |
| 212 | + |
| 213 | + |
| 214 | +%% Run the tests |
| 215 | + |
| 216 | +if (~strcmp(mode, 'silent')) |
| 217 | + disp(['Start Test Case: ', tc.name]); |
| 218 | + disp([' at: ', datestr(now, 31)]); |
| 219 | + disp(' '); |
| 220 | +end |
| 221 | + |
| 222 | + |
| 223 | +%% Start timer |
| 224 | +startWallTime = clock; |
| 225 | +startCPUTime = cputime; |
| 226 | + |
| 227 | +if (run_all_tests) |
| 228 | + tresults = MU_tcase_run(tc, mode); |
| 229 | +else |
| 230 | + tresults = MU_tcase_run(tc, mode, test_name_list); |
| 231 | +end |
| 232 | + |
| 233 | + |
| 234 | +%% Summarize and print test results. |
| 235 | +MU_print_tresults_report(tcname, tresults, mode); |
| 236 | + |
| 237 | +tc_summary = MU_summarize_tresults(tc, tresults); |
| 238 | +MU_print_tcase_summary(tc_summary, mode); |
| 239 | + |
| 240 | +%% Stop timer |
| 241 | +stopWallTime = clock; |
| 242 | +stopCPUTime = cputime; |
| 243 | + |
| 244 | +%% Change back to original direectory |
| 245 | +%% if (~isempty(tc.pathstr)) |
| 246 | +%% cd(curpath); |
| 247 | +%% end |
| 248 | + |
| 249 | + |
| 250 | +elapsedWallTime = etime(stopWallTime, startWallTime); |
| 251 | +elapsedCPUTime = stopCPUTime - startCPUTime; |
| 252 | +tc_summary.elapsedWallTime = elapsedWallTime; |
| 253 | +tc_summary.elapsedCPUTime = elapsedCPUTime; |
| 254 | + |
| 255 | +%% Print tcase results. |
| 256 | + |
| 257 | +if (~strcmp(mode, 'silent')) |
| 258 | + disp(' '); |
| 259 | + disp(['Finished Test Case: ', tc.name]); |
| 260 | + disp([' at: ', datestr(now, 31)]); |
| 261 | + disp([' Elapsed Time: ', num2str(elapsedWallTime), ' sec.']); |
| 262 | + disp([' Elapsed CPU Time: ', num2str(elapsedCPUTime), ' sec.']); |
| 263 | + disp(' '); |
| 264 | +end |
| 265 | + |
| 266 | +diary off; |
| 267 | + |
| 268 | +return |
| 269 | + |
| 270 | + |
0 commit comments