% Copyright 2019 The MathWorks, Inc. % 10-2019, FSchoen: slight adaptions for Robocup CI function failed = runMatlabTests(produceJUnit, produceTAP, produceCobertura) BASE_VERSION_MATLABUNIT_SUPPORT = '8.1'; if verLessThan('matlab',BASE_VERSION_MATLABUNIT_SUPPORT) error('MATLAB:unitTest:testFrameWorkNotSupported','Running tests automatically is not supported in this relase.'); end %Create test suite for tests folder suite = getTestSuite(); % Create and configure the runner import('matlab.unittest.TestRunner'); runner = TestRunner.withTextOutput('Verbosity',4); % Add the requested plugins resultsDir = fullfile(pwd, 'matlabTestArtifacts'); % Produce JUnit report if produceJUnit BASE_VERSION_JUNIT_SUPPORT = '8.6'; if verLessThan('matlab',BASE_VERSION_JUNIT_SUPPORT) warning('MATLAB:testArtifact:junitReportNotSupported', 'Producing JUnit xml results is not supported in this release.'); else import('matlab.unittest.plugins.XMLPlugin'); mkdirIfNeeded(resultsDir) xmlFile = fullfile(resultsDir, 'junittestresults.xml'); runner.addPlugin(XMLPlugin.producingJUnitFormat(xmlFile)); end end % Produce TAP report if produceTAP BASE_VERSION_TAPORIGINALFORMAT_SUPPORT = '8.3'; BASE_VERSION_TAP13_SUPPORT = '9.1'; if verLessThan('matlab',BASE_VERSION_TAPORIGINALFORMAT_SUPPORT) warning('MATLAB:testArtifact:tapReportNotSupported', 'Producing TAP results is not supported in this release.'); tapPlugin = matlab.unittest.plugins.TestRunnerPlugin.empty; elseif verLessThan('matlab',BASE_VERSION_TAP13_SUPPORT) tapFile = getTapResultFile(resultsDir); import('matlab.unittest.plugins.TAPPlugin'); tapPlugin = TAPPlugin.producingOriginalFormat(tapFile); else tapFile = getTapResultFile(resultsDir); import('matlab.unittest.plugins.TAPPlugin'); tapPlugin = TAPPlugin.producingVersion13(tapFile); end runner.addPlugin(tapPlugin); end % Produce Cobertura report (Cobertura report generation is not supported % below R17a) if produceCobertura BASE_VERSION_COBERTURA_SUPPORT = '9.3'; if verLessThan('matlab',BASE_VERSION_COBERTURA_SUPPORT) warning('MATLAB:testArtifact:coberturaReportNotSupported', 'Producing Cobertura results is not supported in this release.'); else import('matlab.unittest.plugins.CodeCoveragePlugin'); import('matlab.unittest.plugins.codecoverage.CoberturaFormat'); mkdirIfNeeded(resultsDir) coverageFile = fullfile(resultsDir, 'cobertura.xml'); workSpace = fullfile(pwd); runner.addPlugin(CodeCoveragePlugin.forFolder(workSpace,'IncludingSubfolders',true,... 'Producing', CoberturaFormat(coverageFile))); end end results = runner.run(suite); failed = any([results.Failed]); function tapToFile = getTapResultFile(resultsDir) import('matlab.unittest.plugins.ToFile'); mkdirIfNeeded(resultsDir) tapFile = fullfile(resultsDir, 'taptestresults.tap'); fclose(fopen(tapFile,'w')); tapToFile = matlab.unittest.plugins.ToFile(tapFile); function suite = getTestSuite() import('matlab.unittest.TestSuite'); suite = TestSuite.fromFolder('CI'); %%% Order tests such that build step will ALWAYS be first % Get index of important build step idx = find(strcmp({suite.Name}', 'makeAndBuildTest/makeAndBuild')); % Get indices of all other test idxs = 1:length(suite); % Reorder test-suite such that build step is first suite = suite([idx,idxs(idxs~=idx)]); function mkdirIfNeeded(dir) if exist(dir,'dir') ~= 7 mkdir(dir); end