diff --git a/SoftwareTests/CreateBadge.m b/SoftwareTests/CreateBadge.m index 9e0a708..c873088 100644 --- a/SoftwareTests/CreateBadge.m +++ b/SoftwareTests/CreateBadge.m @@ -1,82 +1,28 @@ -% Run these tests with runMyTests -% All tests so far are on code expected to run without errors -% If/when we end up with a version that _should_ error, -% please add it to this set of examples -classdef CreateBadge < matlab.unittest.TestCase - - properties - rootProject - results - end - - - methods (TestClassSetup) - - function setUpPath(testCase) - - try - project = currentProject; - testCase.rootProject = project.RootFolder; - cd(testCase.rootProject) - catch - error("Load project prior to run tests") - end - - testCase.log("Running in " + version) - - end % function setUpPath - - function readResults(testCase) - Release = string([]); - Passed = []; - testCase.results = table(Release,Passed); - - ResultFiles = dir("SoftwareTests"+filesep+"TestResults_*"); - for kFiles = 1:size(ResultFiles) - Results = readtable(fullfile(ResultFiles(kFiles).folder,ResultFiles(kFiles).name),... - Delimiter=",",TextType="string"); - Release = Results.Version(1); - Passed = all(Results.Status == "passed"); - testCase.results(end+1,:) = table(Release,Passed); - end - end - - end % methods (TestClassSetup) - - methods(Test) - - function writeBadge(testCase) - - % Create JSON - badgeInfo = struct; - badgeInfo.schemaVersion = 1; - badgeInfo.label = "tested with"; - badgeInfo.message = ""; - - % Check that results exist: - if size(testCase.results,1) == 0 - badgeInfo.message = "None"; - badgeInfo.color = "failed"; - else - for i = 1:size(testCase.results,1) - if testCase.results.Passed(i) - if badgeInfo.message ~= "" - badgeInfo.message = badgeInfo.message + " | "; - end - badgeInfo.message = badgeInfo.message + string(testCase.results.Release(i)); - end - end - badgeInfo.color = "success"; - end - - % Write JSON file out - badgeJSON = jsonencode(badgeInfo); - fid = fopen(fullfile("Images","TestedWith.json"),"w"); - fwrite(fid,badgeJSON); - fclose(fid); - - end - - end - -end \ No newline at end of file +% Create the test suite with SmokeTest and Function test if they exist +Suite = testsuite("CheckTestResults"); + +% Create a runner with no plugins +Runner = matlab.unittest.TestRunner.withNoPlugins; + +% Run the test suite +Results = Runner.run(Suite); + +% Format the results in a table and save them +Results = table(Results'); +Results = Results(Results.Passed,:); +Version = extractBetween(string(Results.Name),"Version=",")"); + + +% Format the JSON file +Badge = struct; +Badge.schemaVersion = 1; +Badge.label = "Tested with"; +if size(Results,1) >= 1 + Badge.color = "success" + Badge.message = join(Version," | "); +else + Badge.color = "failure"; + Badge.message = "Pipeline fails"; +end +Badge = jsonencode(Badge); +writelines(Badge,fullfile("Images","TestedWith.json")); \ No newline at end of file diff --git a/SoftwareTests/RunAllTests.m b/SoftwareTests/RunAllTests.m new file mode 100644 index 0000000..87675d1 --- /dev/null +++ b/SoftwareTests/RunAllTests.m @@ -0,0 +1,44 @@ +function RunAllTest(EnableReport,ReportFolder) +arguments + EnableReport (1,1) logical = false; + ReportFolder (1,1) string = "public"; +end + +import matlab.unittest.plugins.TestReportPlugin; + +% Create a runner +Runner = matlab.unittest.TestRunner.withTextOutput; +if EnableReport + Folder = fullfile(currentProject().RootFolder,ReportFolder); + if ~isfolder(Folder) + mkdir(Folder) + else + rmdir(Folder,'s') + mkdir(Folder) + end + Plugin = TestReportPlugin.producingHTML(Folder,... + "IncludingPassingDiagnostics",true,... + "IncludingCommandWindowText",true,... + "LoggingLevel",matlab.automation.Verbosity(1)); + Runner.addPlugin(Plugin); +end + +% Create the test suite with SmokeTest and Function test if they exist +Suite = testsuite("SmokeTests"); +Suite = [Suite testsuite("FunctionTests")]; + +% Run the test suite +Results = Runner.run(Suite); + +if EnableReport + web(fullfile(Folder,"index.html")) +end + +% Format the results in a table and save them +ResultsTable = table(Results') +writetable(ResultsTable,fullfile("SoftwareTests","TestResults_R"+version("-release")+".txt")); + +% Assert success of test +assertSuccess(Results); + +end