Skip to content

Commit

Permalink
Use custom testTask instead of built-in TestTask to enable passing ex…
Browse files Browse the repository at this point in the history
…ample script filenames as parameter to test class definition.
  • Loading branch information
pablorcum committed Jul 31, 2024
1 parent 4da3d48 commit cd5b6b4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
45 changes: 45 additions & 0 deletions buildUtilities/testTask.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
function results = testTask(tags)
% Run unit tests

arguments
tags string {mustBeText} = "";
end

import matlab.unittest.TestRunner;
import matlab.unittest.TestSuite;
import matlab.unittest.Verbosity;
import matlab.unittest.plugins.XMLPlugin;
import matlab.unittest.parameters.Parameter;

projObj = currentProject;

% Get list of example scripts in path
exName = {dir(fullfile(projObj.RootFolder,'toolbox/examples','**/*.m')).name}';
exName = exName(isFileOnPath(exName));
exParam = Parameter.fromData('exName',exName);

suite = TestSuite.fromProject(projObj,'ExternalParameters',exParam);
if strlength(tags)>0
suite = suite.selectIf("Tag",tags);
else
disp('No tag was passed as input. All test cases will be executed.');
end

if isempty(suite)
warning('No tests were found with tag(s) "%s" and none will be executed.',strjoin(tags,', '));
end

runner = TestRunner.withTextOutput('OutputDetail', Verbosity.Detailed);
runner.addPlugin(XMLPlugin.producingJUnitFormat(fullfile(projObj.RootFolder,'results.xml')));

results = runner.run(suite);
results.assertSuccess;

end

function onPath = isFileOnPath(filename)
onPath = boolean(zeros(1,length(filename)));
for ii = 1:length(filename)
onPath(ii) = exist(filename{ii},"file")>0;
end
end
7 changes: 2 additions & 5 deletions buildfile.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,10 @@
Actions = @(~) updateSGdeps,...
Outputs = "toolbox/dependencies/sg/**");

plan("test") = TestTask( ...
plan("test") = Task( ...
Description = "Run unit tests", ...
Dependencies = ["updateSGdeps","pcodeBossapi"],...
SourceFiles = {'toolbox','tests'},...
TestResults = "results.xml",...
OutputDetail = Verbosity.Detailed,...
Strict = true);
Actions = @(~) testTask);

plan("buildDoc") = Task( ...
Description = "Build HTML doc from sources", ...
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info>
<Category UUID="FileClassCategory">
<Label UUID="design"/>
</Category>
</Info>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<Info location="testTask.m" type="File"/>

0 comments on commit cd5b6b4

Please sign in to comment.