|
| 1 | +classdef SolnSmokeTests < matlab.unittest.TestCase |
| 2 | + |
| 3 | + properties |
| 4 | + RootFolder |
| 5 | + isSolnOnPath |
| 6 | + end |
| 7 | + |
| 8 | + properties (ClassSetupParameter) |
| 9 | + Project = {currentProject()}; |
| 10 | + end |
| 11 | + |
| 12 | + properties (TestParameter) |
| 13 | + File; |
| 14 | + end |
| 15 | + |
| 16 | + methods (TestParameterDefinition,Static) |
| 17 | + |
| 18 | + function File = GetScriptName(Project) |
| 19 | + % Retrieve student template files: |
| 20 | + RootFolder = Project.RootFolder; |
| 21 | + File = dir(fullfile(RootFolder,"Scripts","*.mlx")); |
| 22 | + File = {File.name}; |
| 23 | + end |
| 24 | + |
| 25 | + end |
| 26 | + |
| 27 | + methods (TestClassSetup) |
| 28 | + |
| 29 | + function SetUpPath(testCase,Project) |
| 30 | + % Navigate to project root folder: |
| 31 | + testCase.RootFolder = Project.RootFolder; |
| 32 | + cd(testCase.RootFolder) |
| 33 | + |
| 34 | + % Check that solutions are on path: |
| 35 | + testCase.isSolnOnPath = isfolder("Solutions"); |
| 36 | + if testCase.isSolnOnPath == 0 |
| 37 | + addpath(fullfile(testCase.RootFolder,"InstructorResources","Solutions")) |
| 38 | + end |
| 39 | + |
| 40 | + % Close the StartUp app if still open: |
| 41 | + delete(findall(groot,'Name','StartUp App')) |
| 42 | + |
| 43 | + % Log MATLAB version: |
| 44 | + testCase.log("Running in " + version) |
| 45 | + |
| 46 | + end % function setUpPath |
| 47 | + |
| 48 | + end % methods (TestClassSetup) |
| 49 | + |
| 50 | + methods(Test) |
| 51 | + |
| 52 | + % Check that solutions files exist for each of the student |
| 53 | + % templates |
| 54 | + function ExistSolns(testCase,File) |
| 55 | + SolutionName = replace(string(File),".mlx","Soln.mlx"); |
| 56 | + assert(exist(SolutionName,"file"),"Missing solutions for "+File); |
| 57 | + end |
| 58 | + |
| 59 | + |
| 60 | + function SmokeRun(testCase,File) |
| 61 | + |
| 62 | + % Navigate to project root folder: |
| 63 | + cd(testCase.RootFolder) |
| 64 | + FileToRun = replace(string(File),".mlx","Soln.mlx"); |
| 65 | + |
| 66 | + % Pre-test: |
| 67 | + PreFiles = CheckPreFile(testCase,FileToRun); |
| 68 | + run(PreFiles); |
| 69 | + |
| 70 | + % Run SmokeTest |
| 71 | + disp(">> Running " + FileToRun); |
| 72 | + try |
| 73 | + run(fullfile("InstructorResources","Solutions",FileToRun)); |
| 74 | + catch ME |
| 75 | + |
| 76 | + end |
| 77 | + |
| 78 | + % Post-test: |
| 79 | + PostFiles = CheckPostFile(testCase,FileToRun); |
| 80 | + run(PostFiles) |
| 81 | + |
| 82 | + % Log every figure created during run: |
| 83 | + Figures = findall(groot,'Type','figure'); |
| 84 | + Figures = flipud(Figures); |
| 85 | + if ~isempty(Figures) |
| 86 | + for f = 1:size(Figures,1) |
| 87 | + if ~isempty(Figures(f).Number) |
| 88 | + FigDiag = matlab.unittest.diagnostics.FigureDiagnostic(Figures(f),'Formats','png'); |
| 89 | + log(testCase,1,FigDiag); |
| 90 | + end |
| 91 | + end |
| 92 | + end |
| 93 | + |
| 94 | + % Close all figures and Simulink models |
| 95 | + close all force |
| 96 | + if any(matlab.addons.installedAddons().Name == "Simulink") |
| 97 | + bdclose all |
| 98 | + end |
| 99 | + |
| 100 | + % Rethrow error if any |
| 101 | + if exist("ME","var") |
| 102 | + if ~any(strcmp(ME.identifier,KnownIssuesID)) |
| 103 | + rethrow(ME) |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + end |
| 108 | + |
| 109 | + end |
| 110 | + |
| 111 | + methods (Access = private) |
| 112 | + |
| 113 | + function Path = CheckPreFile(testCase,Filename) |
| 114 | + PreFile = "Pre"+replace(Filename,".mlx",".m"); |
| 115 | + PreFilePath = fullfile(testCase.RootFolder,"SoftwareTests","PreFiles",PreFile); |
| 116 | + if ~isfolder(fullfile(testCase.RootFolder,"SoftwareTests/PreFiles")) |
| 117 | + mkdir(fullfile(testCase.RootFolder,"SoftwareTests/PreFiles")) |
| 118 | + end |
| 119 | + if ~isfile(PreFilePath) |
| 120 | + writelines("% Pre-run script for "+Filename,PreFilePath) |
| 121 | + writelines("% ---- Known Issues -----",PreFilePath,'WriteMode','append'); |
| 122 | + writelines("KnownIssuesID = "+char(34)+char(34)+";",PreFilePath,'WriteMode','append'); |
| 123 | + writelines("% ---- Pre-run commands -----",PreFilePath,'WriteMode','append'); |
| 124 | + writelines(" ",PreFilePath,'WriteMode','append'); |
| 125 | + end |
| 126 | + Path = PreFilePath; |
| 127 | + end |
| 128 | + |
| 129 | + function Path = CheckPostFile(testCase,Filename) |
| 130 | + PostFile = "Post"+replace(Filename,".mlx",".m"); |
| 131 | + PostFilePath = fullfile(testCase.RootFolder,"SoftwareTests","PostFiles",PostFile); |
| 132 | + if ~isfolder(fullfile(testCase.RootFolder,"SoftwareTests/PostFiles")) |
| 133 | + mkdir(fullfile(testCase.RootFolder,"SoftwareTests/PostFiles")) |
| 134 | + end |
| 135 | + if ~isfile(PostFilePath) |
| 136 | + writelines("% Post-run script for "+Filename,PostFilePath) |
| 137 | + writelines("% ---- Post-run commands -----",PostFilePath,'WriteMode','append'); |
| 138 | + writelines(" ",PostFilePath,'WriteMode','append'); |
| 139 | + end |
| 140 | + Path = PostFilePath; |
| 141 | + end |
| 142 | + |
| 143 | + end |
| 144 | + |
| 145 | +end |
0 commit comments