Skip to content

Commit

Permalink
Update testAll.m
Browse files Browse the repository at this point in the history
  • Loading branch information
farid-zare authored Feb 2, 2025
1 parent 264214f commit d012f59
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions test/testAll.m
Original file line number Diff line number Diff line change
Expand Up @@ -188,52 +188,72 @@

fprintf(['\n > ', num2str(sumFailed), ' tests failed. ', num2str(sumSkipped), ' tests were skipped due to missing requirements.\n\n']);
%% NEW: Generate JUnit XML report for Codecov

xmlFileName = 'CodeCovTestResults.xml';
fid = fopen(xmlFileName, 'w');
if fid == -1
error('Could not open file for writing: %s', xmlFileName);
end

fprintf(fid, '<?xml version="1.0" encoding="UTF-8"?>\n');
numTests = height(resultTable);

numTests = height(resultTable);
numFailures = sum(resultTable.Failed);
numSkipped = sum(resultTable.Skipped);
totalTime = 0;
for i = 1:numTests
if isnan(resultTable.Time(i))
tVal = 0;
else
tVal = resultTable.Time(i);
end
totalTime = totalTime + tVal;
end
numErrors = sum(resultTable.Failed);
numSkipped = sum(resultTable.Skipped);

% Compute total time and also count how many are "failures" vs. "errors"
totalTime = sum(resultTable.Time);

fprintf(fid, '<testsuite name="COBRA Toolbox Test Suite" tests="%d" failures="%d" skipped="%d" time="%.3f">\n', ...
numTests, numFailures, numSkipped, totalTime);
% 1) Wrap in <testsuites> -- typical JUnit format
fprintf(fid, '<testsuites name="COBRA Toolbox Test Suites" tests="%d" failures="%d" errors="%d" time="%.3f">\n', ...
numTests, numFailures, numErrors, totalTime);

% 2) A single <testsuite> inside
fprintf(fid, ' <testsuite name="COBRA Toolbox Test Suite" tests="%d" failures="%d" errors="%d" skipped="%d" time="%.3f">\n', ...
numTests, numFailures, numErrors, numSkipped, totalTime);

% 3) Loop over each test case
for i = 1:numTests
testName = resultTable.TestName{i};
if isnan(resultTable.Time(i))
tVal = 0;
else
tVal = resultTable.Time(i);
end
fprintf(fid, ' <testcase classname="COBRA Toolbox" name="%s" time="%.3f"', testName, tVal);

% Start the <testcase> tag
fprintf(fid, ' <testcase classname="COBRA Toolbox" name="%s" time="%.3f"', testName, tVal);

if resultTable.Passed(i)
% Passed => just close
fprintf(fid, '/>\n');
else
elseif resultTable.Skipped(i)
% Skipped => <skipped/>
fprintf(fid, '>\n');
if resultTable.Skipped(i)
fprintf(fid, ' <skipped message="%s"/>\n', escapeXML(resultTable.Details{i}));
fprintf(fid, ' <skipped message="%s"/>\n', escapeXML(resultTable.Details{i}));
fprintf(fid, ' </testcase>\n');
else
% Not passed, not skipped => either <failure> or <error>
% Check the .Error or .Details to decide
errMsg = result(i).Error.message; % or getReport()

% Heuristic: if "Assertion" => <failure>, else <error>.
if contains(errMsg, 'Assertion') || contains(errMsg, 'assert')
fprintf(fid, '>\n');
fprintf(fid, ' <failure message="%s"/>\n', escapeXML(errMsg));
else
fprintf(fid, ' <failure message="%s"/>\n', escapeXML(resultTable.Details{i}));
fprintf(fid, '>\n');
fprintf(fid, ' <error message="%s"/>\n', escapeXML(errMsg));
end
fprintf(fid, ' </testcase>\n');

fprintf(fid, ' </testcase>\n');
end
end

fprintf(fid, '</testsuite>\n');
% Close out the suite and suites
fprintf(fid, ' </testsuite>\n');
fprintf(fid, '</testsuites>\n');
fclose(fid);
%% End of XML generation

Expand Down

0 comments on commit d012f59

Please sign in to comment.