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