|
1 | | -%Copyright 2019-2020 The MathWorks, Inc. |
| 1 | +%Copyright 2020 The MathWorks, Inc. |
2 | 2 |
|
3 | | -function failed = runMatlabTests(varargin) |
| 3 | +function runMatlabTests(varargin) |
4 | 4 |
|
5 | 5 | p = inputParser; |
6 | 6 | validationFcn = @(c)ischar(c) && (isempty(c) || isrow(c)); |
|
14 | 14 |
|
15 | 15 | p.parse(varargin{:}); |
16 | 16 |
|
17 | | - |
18 | 17 | pdfReportPath = p.Results.PDFReportPath; |
19 | 18 | tapReportPath = p.Results.TAPResultsPath; |
20 | 19 | junitReportPath = p.Results.JUnitResultsPath; |
21 | 20 | stmReportPath = p.Results.SimulinkTestResultsPath; |
22 | 21 | coberturaReportPath = p.Results.CoberturaCodeCoveragePath; |
23 | 22 | modelCoveragePath = p.Results.CoberturaModelCoveragePath; |
24 | 23 |
|
25 | | -BASE_VERSION_MATLABUNIT_SUPPORT = '8.1'; |
26 | | - |
27 | | -if verLessThan('matlab',BASE_VERSION_MATLABUNIT_SUPPORT) |
28 | | - error('MATLAB:unitTest:testFrameWorkNotSupported','Running tests automatically is not supported in this relase.'); |
29 | | -end |
30 | | - |
31 | | -%Create test suite for tests folder |
32 | | -suite = getTestSuite(); |
33 | | - |
34 | | -% Create and configure the runner |
35 | | -import('matlab.unittest.TestRunner'); |
36 | | -runner = TestRunner.withTextOutput; |
37 | | - |
38 | | - |
39 | | - |
40 | | -% Produce JUnit report |
41 | | -if ~isempty(junitReportPath) |
42 | | - BASE_VERSION_JUNIT_SUPPORT = '8.6'; |
43 | | - if verLessThan('matlab',BASE_VERSION_JUNIT_SUPPORT) |
44 | | - warning('MATLAB:testArtifact:junitReportNotSupported', 'Producing JUnit xml results is not supported in this release.'); |
45 | | - else |
46 | | - import('matlab.unittest.plugins.XMLPlugin'); |
47 | | - preparePath(junitReportPath); |
48 | | - runner.addPlugin(XMLPlugin.producingJUnitFormat(junitReportPath)); |
49 | | - end |
50 | | -end |
51 | | - |
52 | | -% Produce TAP report |
53 | | -if ~isempty(tapReportPath) |
54 | | - BASE_VERSION_TAPORIGINALFORMAT_SUPPORT = '8.3'; |
55 | | - BASE_VERSION_TAP13_SUPPORT = '9.1'; |
56 | | - if verLessThan('matlab',BASE_VERSION_TAPORIGINALFORMAT_SUPPORT) |
57 | | - warning('MATLAB:testArtifact:tapReportNotSupported', 'Producing TAP results is not supported in this release.'); |
58 | | - elseif verLessThan('matlab',BASE_VERSION_TAP13_SUPPORT) |
59 | | - tapFile = getTapResultFile(tapReportPath); |
60 | | - import('matlab.unittest.plugins.TAPPlugin'); |
61 | | - tapPlugin = TAPPlugin.producingOriginalFormat(tapFile); |
62 | | - runner.addPlugin(tapPlugin); |
63 | | - else |
64 | | - tapFile = getTapResultFile(tapReportPath); |
65 | | - import('matlab.unittest.plugins.TAPPlugin'); |
66 | | - tapPlugin = TAPPlugin.producingVersion13(tapFile); |
67 | | - runner.addPlugin(tapPlugin); |
68 | | - end |
69 | | - |
70 | | -end |
71 | | - |
72 | | -% Produce Cobertura report (Cobertura report generation is not supported |
73 | | -% below R17a) |
74 | | -if ~isempty(coberturaReportPath) |
75 | | - BASE_VERSION_COBERTURA_SUPPORT = '9.3'; |
76 | | - |
77 | | - if verLessThan('matlab',BASE_VERSION_COBERTURA_SUPPORT) |
78 | | - warning('MATLAB:testArtifact:coberturaReportNotSupported', 'Producing Cobertura code coverage results is not supported in this release.'); |
79 | | - else |
80 | | - import('matlab.unittest.plugins.CodeCoveragePlugin'); |
81 | | - preparePath(coberturaReportPath); |
82 | | - workSpace = fullfile(pwd); |
83 | | - runner.addPlugin(CodeCoveragePlugin.forFolder(workSpace,'IncludingSubfolders',true,... |
84 | | - 'Producing', CoberturaFormat(coberturaReportPath))); |
85 | | - end |
86 | | -end |
87 | | - |
88 | | -% Produce Cobertura model coverage report (Not supported below R2018b) |
89 | | -if ~isempty(modelCoveragePath) |
90 | | - if ~exist('sltest.plugins.ModelCoveragePlugin', 'class') || ~coberturaModelCoverageSupported |
91 | | - warning('MATLAB:testArtifact:cannotGenerateModelCoverageReport', ... |
92 | | - 'Unable to generate Cobertura model coverage report. To generate the report, use a Simulink Coverage license with MATLAB R2018b or a newer release.'); |
93 | | - else |
94 | | - import('sltest.plugins.ModelCoveragePlugin'); |
95 | | - |
96 | | - preparePath(modelCoveragePath); |
97 | | - runner.addPlugin(ModelCoveragePlugin('Producing',CoberturaFormat(modelCoveragePath))); |
98 | | - end |
99 | | -end |
100 | | - |
101 | | -stmResultsPluginAddedToRunner = false; |
102 | | - |
103 | | -% Save Simulink Test Manager results in MLDATX format (Not supported below R2019a) |
104 | | -if ~isempty(stmReportPath) |
105 | | - if ~stmResultsPluginPresent || ~exportSTMResultsSupported |
106 | | - issueExportSTMResultsUnsupportedWarning; |
107 | | - else |
108 | | - preparePath(stmReportPath); |
109 | | - runner.addPlugin(TestManagerResultsPlugin('ExportToFile', stmReportPath)); |
110 | | - stmResultsPluginAddedToRunner = true; |
111 | | - end |
112 | | -end |
113 | | - |
114 | | -% Produce PDF test report (Not supported on MacOS platforms and below R2017a) |
115 | | -if ~isempty(pdfReportPath) |
116 | | - if ismac |
117 | | - warning('MATLAB:testArtifact:unSupportedPlatform', ... |
118 | | - 'Producing a PDF test report is not currently supported on MacOS platforms.'); |
119 | | - elseif ~testReportPluginPresent |
120 | | - issuePDFReportUnsupportedWarning; |
121 | | - else |
122 | | - preparePath(pdfReportPath); |
123 | | - import('matlab.unittest.plugins.TestReportPlugin'); |
124 | | - runner.addPlugin(TestReportPlugin.producingPDF(pdfReportPath)); |
125 | | - |
126 | | - if ~stmResultsPluginAddedToRunner && stmResultsPluginPresent |
127 | | - runner.addPlugin(TestManagerResultsPlugin); |
128 | | - end |
129 | | - end |
130 | | -end |
131 | | - |
132 | | -results = runner.run(suite); |
133 | | -failed = any([results.Failed]); |
134 | | - |
135 | | -function preparePath(path) |
136 | | -dir = fileparts(path); |
137 | | -dirExists = isempty(dir) || exist(dir,'dir') == 7; |
138 | | -if ~dirExists |
139 | | - mkdir(dir); |
140 | | -end |
141 | | - |
142 | | -function tapFile = getTapResultFile(resultsDir) |
143 | | -import('matlab.unittest.plugins.ToFile'); |
144 | | -preparePath(resultsDir); |
145 | | -fclose(fopen(resultsDir,'w')); |
146 | | -tapFile = matlab.unittest.plugins.ToFile(resultsDir); |
147 | | - |
148 | | -function suite = getTestSuite() |
149 | | -import('matlab.unittest.TestSuite'); |
150 | | -BASE_VERSION_TESTSUITE_SUPPORT = '9.0'; |
151 | | -if verLessThan('matlab',BASE_VERSION_TESTSUITE_SUPPORT) |
152 | | - suite = matlab.unittest.TestSuite.fromFolder(pwd,'IncludingSubfolders',true); |
153 | | -else |
154 | | - suite = testsuite(pwd,'IncludeSubfolders',true); |
155 | | -end |
156 | | - |
157 | | -function plugin = CoberturaFormat(varargin) |
158 | | -plugin = matlab.unittest.plugins.codecoverage.CoberturaFormat(varargin{:}); |
159 | | - |
160 | | -function plugin = TestManagerResultsPlugin(varargin) |
161 | | -plugin = sltest.plugins.TestManagerResultsPlugin(varargin{:}); |
162 | | - |
163 | | -function tf = testReportPluginPresent |
164 | | -BASE_VERSION_REPORTPLUGIN_SUPPORT = '9.2'; % R2017a |
165 | | - |
166 | | -tf = ~verLessThan('matlab',BASE_VERSION_REPORTPLUGIN_SUPPORT); |
167 | | - |
168 | | -function tf = stmResultsPluginPresent |
169 | | -tf = logical(exist('sltest.plugins.TestManagerResultsPlugin', 'class')); |
170 | | - |
171 | | -function tf = coberturaModelCoverageSupported |
172 | | -BASE_VERSION_MODELCOVERAGE_SUPPORT = '9.5'; % R2018b |
173 | | - |
174 | | -tf = ~verLessThan('matlab',BASE_VERSION_MODELCOVERAGE_SUPPORT); |
175 | | - |
176 | | -function tf = exportSTMResultsSupported |
177 | | -BASE_VERSION_EXPORTSTMRESULTS_SUPPORT = '9.6'; % R2019a |
178 | | - |
179 | | -tf = ~verLessThan('matlab',BASE_VERSION_EXPORTSTMRESULTS_SUPPORT); |
180 | | - |
181 | | -function issuePDFReportUnsupportedWarning |
182 | | -warning('MATLAB:testArtifact:pdfReportNotSupported', ... |
183 | | - 'Producing a test report in PDF format is not supported in the current MATLAB release.'); |
| 24 | +testScript = genscript('Test','PDFTestReport',pdfReportPath,'TAPTestResults',tapReportPath,'JUnitTestResults',junitReportPath,'SimulinkTestResults',stmReportPath,'CoberturaCodeCoverage',coberturaReportPath,'CoberturaModelCoverage',modelCoveragePath); |
184 | 25 |
|
185 | | -function issueExportSTMResultsUnsupportedWarning |
186 | | -warning('MATLAB:testArtifact:cannotExportSimulinkTestManagerResults', ... |
187 | | - ['Unable to export Simulink Test Manager results. This feature ', ... |
188 | | - 'requires a Simulink Test license and is supported only in MATLAB R2019a or a newer release.']); |
| 26 | +disp('Running MATLAB script with content:\n'); |
| 27 | +disp(strtrim(testScript.writeToText())); |
| 28 | +fprintf('___________________________________\n\n'); |
| 29 | +run(testScript); |
0 commit comments