spm_tests.m 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. function results = spm_tests(varargin)
  2. % Unit Testing Framework
  3. % FORMAT results = spm_tests(name,value,...)
  4. % name,value - pairs of optional parameter names and values:
  5. % verbose: verbosity level of test run progress report [default: 2]
  6. % display: display test results [default: false]
  7. % coverage: display code coverage [default: false]
  8. % cobertura: save code coverage results in the Cobertura XML format [default: false]
  9. % tag: test tag selector [default: '', ie all tests]
  10. % tap: save a Test Anything Protocol (TAP) file [default: false]
  11. % test: name of function to test [default: '', ie all tests]
  12. %
  13. % results - TestResult array containing information describing the
  14. % result of running the test suite.
  15. %__________________________________________________________________________
  16. % Copyright (C) 2015-2017 Wellcome Trust Centre for Neuroimaging
  17. % Guillaume Flandin
  18. % $Id: spm_tests.m 7109 2017-06-15 11:15:23Z guillaume $
  19. if spm_check_version('matlab','8.3') < 0
  20. error('Unit Tests require MATLAB R2014a or above.');
  21. end
  22. SVNid = '$Rev: 7109 $';
  23. SPMid = spm('FnBanner',mfilename,SVNid);
  24. %-Input parameters
  25. %--------------------------------------------------------------------------
  26. options = struct('verbose',2, 'display',false, 'coverage',false, ...
  27. 'cobertura',false, 'tag', '', 'tap',false, 'test','');
  28. if nargin
  29. if isstruct(varargin{1})
  30. fn = fieldnames(varargin{1});
  31. for i=1:numel(fn)
  32. options.(fn{i}) = varargin{1}.(fn{i});
  33. end
  34. else
  35. for i=1:2:numel(varargin)
  36. options.(varargin{i}) = varargin{i+1};
  37. end
  38. end
  39. end
  40. %-Unit tests
  41. %==========================================================================
  42. %-Create a TestSuite
  43. %--------------------------------------------------------------------------
  44. import matlab.unittest.TestSuite;
  45. import matlab.unittest.selectors.*;
  46. tests = fullfile(spm('Dir'),'tests');
  47. if isempty(options.test)
  48. suite = TestSuite.fromFolder(tests, 'IncludingSubfolders', true);
  49. else
  50. suite = [];
  51. options.test = cellstr(options.test);
  52. for i=1:numel(options.test)
  53. prefix = 'test_';
  54. if strncmp(options.test{i},prefix,length(prefix))
  55. prefix = '';
  56. end
  57. mtest = fullfile(tests,[prefix spm_file(options.test{i},'ext','.m')]);
  58. if ~spm_existfile(mtest)
  59. warning('SPM:tests:fileNotFound',...
  60. 'No tests found for %s',options.test{i});
  61. continue
  62. end
  63. suite = [suite, TestSuite.fromFile(mtest)];
  64. % if i==1
  65. % suite = TestSuite.fromFile(mtest);
  66. % else
  67. % suite(i) = TestSuite.fromFile(mtest);
  68. % end
  69. end
  70. end
  71. if ~isempty(options.tag)
  72. suite = suite.selectIf(~HasTag | HasTag(options.tag));
  73. end
  74. %-Create a TestRunner
  75. %--------------------------------------------------------------------------
  76. import matlab.unittest.TestRunner;
  77. import matlab.unittest.plugins.*
  78. if ~options.verbose
  79. runner = TestRunner.withNoPlugins;
  80. else
  81. runner = TestRunner.withTextOutput('Verbosity',options.verbose);
  82. end
  83. if options.coverage
  84. plugin = CodeCoveragePlugin.forFolder(spm('Dir'));
  85. runner.addPlugin(plugin);
  86. end
  87. if options.cobertura
  88. d = getenv('WORKSPACE');
  89. if isempty(d), d = spm('Dir'); end
  90. coberturaFile = fullfile(d,'spm_CoverageResults.xml');
  91. plugin = CodeCoveragePlugin.forFolder(spm('Dir'),...
  92. 'Producing',codecoverage.CoberturaFormat(coberturaFile));
  93. runner.addPlugin(plugin);
  94. end
  95. if options.tap
  96. d = getenv('WORKSPACE');
  97. if isempty(d), d = spm('Dir'); end
  98. tapFile = fullfile(d,'spm_tests.tap');
  99. plugin = TAPPlugin.producingVersion13(ToFile(tapFile));
  100. runner.addPlugin(plugin);
  101. end
  102. %-Run tests
  103. %--------------------------------------------------------------------------
  104. results = runner.run(suite);
  105. %-Display test results
  106. %--------------------------------------------------------------------------
  107. if options.display
  108. fprintf(['Totals (%d tests):\n\t%d Passed, %d Failed, %d Incomplete.\n' ...
  109. '\t%f seconds testing time.\n\n'],numel(results),nnz([results.Passed]),...
  110. nnz([results.Failed]),nnz([results.Incomplete]),sum([results.Duration]));
  111. disp(table(results));
  112. end