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