Skip to content

Commit 242999a

Browse files
committed
Add RunAllTests and CreateBadge
1 parent e94a56a commit 242999a

File tree

2 files changed

+72
-82
lines changed

2 files changed

+72
-82
lines changed

SoftwareTests/CreateBadge.m

+28-82
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,28 @@
1-
% Run these tests with runMyTests
2-
% All tests so far are on code expected to run without errors
3-
% If/when we end up with a version that _should_ error,
4-
% please add it to this set of examples
5-
classdef CreateBadge < matlab.unittest.TestCase
6-
7-
properties
8-
rootProject
9-
results
10-
end
11-
12-
13-
methods (TestClassSetup)
14-
15-
function setUpPath(testCase)
16-
17-
try
18-
project = currentProject;
19-
testCase.rootProject = project.RootFolder;
20-
cd(testCase.rootProject)
21-
catch
22-
error("Load project prior to run tests")
23-
end
24-
25-
testCase.log("Running in " + version)
26-
27-
end % function setUpPath
28-
29-
function readResults(testCase)
30-
Release = string([]);
31-
Passed = [];
32-
testCase.results = table(Release,Passed);
33-
34-
ResultFiles = dir("SoftwareTests"+filesep+"TestResults_*");
35-
for kFiles = 1:size(ResultFiles)
36-
Results = readtable(fullfile(ResultFiles(kFiles).folder,ResultFiles(kFiles).name),...
37-
Delimiter=",",TextType="string");
38-
Release = Results.Version(1);
39-
Passed = all(Results.Status == "passed");
40-
testCase.results(end+1,:) = table(Release,Passed);
41-
end
42-
end
43-
44-
end % methods (TestClassSetup)
45-
46-
methods(Test)
47-
48-
function writeBadge(testCase)
49-
50-
% Create JSON
51-
badgeInfo = struct;
52-
badgeInfo.schemaVersion = 1;
53-
badgeInfo.label = "tested with";
54-
badgeInfo.message = "";
55-
56-
% Check that results exist:
57-
if size(testCase.results,1) == 0
58-
badgeInfo.message = "None";
59-
badgeInfo.color = "failed";
60-
else
61-
for i = 1:size(testCase.results,1)
62-
if testCase.results.Passed(i)
63-
if badgeInfo.message ~= ""
64-
badgeInfo.message = badgeInfo.message + " | ";
65-
end
66-
badgeInfo.message = badgeInfo.message + string(testCase.results.Release(i));
67-
end
68-
end
69-
badgeInfo.color = "success";
70-
end
71-
72-
% Write JSON file out
73-
badgeJSON = jsonencode(badgeInfo);
74-
fid = fopen(fullfile("Images","TestedWith.json"),"w");
75-
fwrite(fid,badgeJSON);
76-
fclose(fid);
77-
78-
end
79-
80-
end
81-
82-
end
1+
% Create the test suite with SmokeTest and Function test if they exist
2+
Suite = testsuite("CheckTestResults");
3+
4+
% Create a runner with no plugins
5+
Runner = matlab.unittest.TestRunner.withNoPlugins;
6+
7+
% Run the test suite
8+
Results = Runner.run(Suite);
9+
10+
% Format the results in a table and save them
11+
Results = table(Results');
12+
Results = Results(Results.Passed,:);
13+
Version = extractBetween(string(Results.Name),"Version=",")");
14+
15+
16+
% Format the JSON file
17+
Badge = struct;
18+
Badge.schemaVersion = 1;
19+
Badge.label = "Tested with";
20+
if size(Results,1) >= 1
21+
Badge.color = "success"
22+
Badge.message = join(Version," | ");
23+
else
24+
Badge.color = "failure";
25+
Badge.message = "Pipeline fails";
26+
end
27+
Badge = jsonencode(Badge);
28+
writelines(Badge,fullfile("Images","TestedWith.json"));

SoftwareTests/RunAllTests.m

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
function RunAllTest(EnableReport,ReportFolder)
2+
arguments
3+
EnableReport (1,1) logical = false;
4+
ReportFolder (1,1) string = "public";
5+
end
6+
7+
import matlab.unittest.plugins.TestReportPlugin;
8+
9+
% Create a runner
10+
Runner = matlab.unittest.TestRunner.withTextOutput;
11+
if EnableReport
12+
Folder = fullfile(currentProject().RootFolder,ReportFolder);
13+
if ~isfolder(Folder)
14+
mkdir(Folder)
15+
else
16+
rmdir(Folder,'s')
17+
mkdir(Folder)
18+
end
19+
Plugin = TestReportPlugin.producingHTML(Folder,...
20+
"IncludingPassingDiagnostics",true,...
21+
"IncludingCommandWindowText",true,...
22+
"LoggingLevel",matlab.automation.Verbosity(1));
23+
Runner.addPlugin(Plugin);
24+
end
25+
26+
% Create the test suite with SmokeTest and Function test if they exist
27+
Suite = testsuite("SmokeTests");
28+
Suite = [Suite testsuite("FunctionTests")];
29+
30+
% Run the test suite
31+
Results = Runner.run(Suite);
32+
33+
if EnableReport
34+
web(fullfile(Folder,"index.html"))
35+
end
36+
37+
% Format the results in a table and save them
38+
ResultsTable = table(Results')
39+
writetable(ResultsTable,fullfile("SoftwareTests","TestResults_R"+version("-release")+".txt"));
40+
41+
% Assert success of test
42+
assertSuccess(Results);
43+
44+
end

0 commit comments

Comments
 (0)