Skip to content
Snippets Groups Projects
Commit 757df600 authored by Federico Felici's avatar Federico Felici
Browse files

Refactor test runner and remove rtccode ones

parent 8777569e
No related branches found
No related tags found
No related merge requests found
function [passed,results] = run_rtccode_tests(test_case)
% test runner for rtccode
% F. Felici, EPFL federico.felici@epfl.ch
function [passed,results] = run_scdds_core_tests(test_case, coverage_report)
if nargin==0 || isempty(test_case)
test_case = 'algos'; % default
test_case = 'basic'; % default
end
if nargin < 2
coverage_report = false; % default
end
test_case = lower(test_case);
if coverage_report
assert(~verLessThan('matlab','9.6.0'),'coverage_report=true needs MATLAB 9.6.0 or later');
assert( strcmpi(test_case,'coverage'),'coverage_report=true should only be used with test_case=''coverage''');
end
needXML=~isempty(getenv('GITLAB_CI')) && ~verLessThan('matlab','8.6.0');
needCOV=~isempty(getenv('GITLAB_CI')) && ~verLessThan('matlab','9.6.0') || coverage_report;
%% Default outputs
passed=false; results=[];
%% Import some classes we need
import matlab.unittest.selectors.HasTag;
import matlab.unittest.constraints.ContainsSubstring;
import matlab.unittest.selectors.HasName;
import matlab.unittest.TestRunner
%% Paths
% add path of toolbox to make sure it is in the path
tbxpath = fileparts(mfilename('fullpath'));
projectpath = getenv('CI_PROJECT_DIR');
if isempty(projectpath), projectpath = tbxpath; end
%% initialize paths
scdds_core_paths;
addpath('tests');
%% Name
tbxname = getenv('TBXTARGET');
if isempty(tbxname)
% Fallback value: name of folder
[~,tbxname] = fileparts(tbxpath);
end
%% populate suite
%% Generate test suite
testspath = fullfile(tbxpath,'tests');
lastwarn('',''); % clear last warning
suite_all = matlab.unittest.TestSuite.fromFolder(testspath);
[~,s] = lastwarn();
if isequal(s,'MATLAB:unittest:TestSuite:FileExcluded')
fprintf('File excluded during test suite creation - possible syntax errors in a test class\n');
return
end
% Identify tests without any tags
mask = cellfun(@isempty,{suite_all.Tags});
if any(mask)
fprintf('Some tests do not have TestTags set and are unreachable from run_meq_tests:\n');
fprintf('\t%s\n',suite_all(mask).Name);
return
end
useparallel = false; % switch for parallel testing using parallel toolbox
paravail = ~isempty(ver('distcomp')); % check if parallel toolbox is installed
fprintf('running %s\n',mfilename('fullpath'))
switch lower(test_case)
case 'algos'
fprintf('populating test suite with all SCDalgo tests... \n')
suite = testsuite('algos','IncludingSubfolders',true,'superclass','SCDalgo_test');
fprintf('done \n')
case 'unit'
s = HasTag('Unit');
suite = [matlab.unittest.TestSuite.fromFolder('tests',s),...
testsuite(fullfile('classes'),'IncludingSubfolders',true)];
case {'unit'}
s = HasTag('unit');
case {'algos'}
% add algos folder tests
suite_algos = testsuite('algos','IncludingSubfolders',true,'superclass','SCDalgo_test');
suite_all = [suite_all,suite_algos];
s = HasTag('algos');
otherwise
error('unknown test_case %s',test_case)
% assume tag with this name
s = HasTag(test_case);
end
suite = suite_all.selectIf(s);
assert(~isempty(suite),'empty test suite');
if isempty(suite)
fprintf('\nEmpty test suite returned for TestTag=''%s''\n',test_case); return;
end
%% run it
fprintf('Start test case: %s\n%s\n\n',test_case,datestr(now));
results = run(suite);
disp(table(results));
fprintf('Main Test File: %s\nStart test case: %s\n%s\n\n',mfilename('fullpath'),test_case,datestr(now));
runner = matlab.unittest.TestRunner.withTextOutput;
if needXML || needCOV
prefix = sprintf('test_%s',tbxname);
suffix = version('-release');
end
if needXML
% Add some JUnit XML file with tests results
xmlFile = fullfile(projectpath,sprintf('%s_%s_%s.xml',prefix,test_case,suffix));
p = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat(xmlFile);
runner.addPlugin(p)
end
if needCOV
% Add some code coverage report
switch lower(test_case)
case 'coverage'
% Produce HTML report
reportFolder = fullfile(projectpath,sprintf('%s_%s_cov',prefix,suffix));
reportFormat = matlab.unittest.plugins.codecoverage.CoverageReport(reportFolder);
otherwise
% Produce XML file in Cobertura format (for Gitlab MR viz.)
xmlFile = fullfile(projectpath,sprintf('%s_%s_%s_cov.xml',prefix,test_case,suffix));
reportFormat = matlab.unittest.plugins.codecoverage.CoberturaFormat(xmlFile);
end
p = matlab.unittest.plugins.CodeCoveragePlugin.forFolder({tbxpath,fullfile(tbxpath,'mexm'),testspath},...
'IncludingSubfolders',false,'Producing',reportFormat);
runner.addPlugin(p)
end
if useparallel && paravail
results = runInParallel(runner,suite);
else
results = run(runner,suite);
end
fprintf('\nTotal test duration: %5.2fs\n',sum(table(results).Duration))
%%
% close any parallel pools
if paravail, delete(gcp('nocreate')); end
%% Display results
fprintf('Passed tests: \n');
disp(table(results([results.Passed])));
if all([results.Passed])
fprintf('\nPassed all tests\n')
passed = true;
......@@ -52,5 +134,8 @@ elseif any([results.Incomplete])
disp(table(results([results.Incomplete])));
passed = true; % pass tests even if some were skipped
else
% the conditions above should cover all cases, otherwise
error('something is very wrong - please check')
end
end
......@@ -35,7 +35,7 @@ classdef SCDalgo_test < SCDtest
end
end
methods (Test)
methods (Test,TestTags={'algos'})
function test_printinfo(testCase)
testCase.algoobj.printinfo;
......
......@@ -46,7 +46,7 @@ classdef SCDsignal_test < matlab.unittest.TestCase
end
end
methods(Test)
methods(Test,TestTags={'unit'})
function test_bus_size(testCase)
% test size of bus
S = testCase.sig;
......
classdef test_SCDclass < SCDtest
% test file for SCD interface class
properties
expcode = 1;
shot = 65668;
end
methods(Test)
function test_load(testCase)
SCD.load(testCase.expcode);
end
function test_init(testCase)
SCD.init(testCase.expcode,testCase.shot);
end
end
end
\ No newline at end of file
classdef test_expcodes < SCDtest
properties
expcode_obj;
SCDexps;
shot;
end
properties(ClassSetupParameter)
expcode = {'1','1005','1007','1008','1010'}; % list of expcodes to test
shote = {'68915','68915','70127','68915','68915'}; % different shot per exp code
%expcode = {'1'}; % list of expcodes to test
%shote = {'68915'}; % different shot per exp code
end
methods(TestClassSetup,ParameterCombination='sequential')
function setup_environment(testCase)
testCase.addTeardown(@cd,pwd);
testCase.addTeardown(@path,path);
testCase.addTeardown(@() SCDclass_expcode.close_all(0))
% Clean data dictionaries
SCD.clean_dd;
basePath = fullfile(fileparts(mfilename('fullpath')),'..','..');
run(fullfile(basePath,'rtccode_paths'));
% clear base workspace
evalin('base','clear variables');
% get SCD experimental code object container
testCase.SCDexps = SCDconf_createexpcodes;
SCDconf_setConf('SIM'); % set ConfigurationSettings for Simulation
end
function setup_expcode(testCase,expcode,shote)
% get this expcode object from expcode object container
fprintf('\n=== Testing expcode %s ===\n',expcode);
testCase.expcode_obj = getbymaincode(testCase.SCDexps,str2double(expcode));
testCase.shot = str2double(shote);
end
end
methods(Test,TestTags={'expcodes'})
function test_expcode_printinfo(testCase)
testCase.expcode_obj.printinfo
end
function test_expcode_init(testCase)
fprintf('\n=== Testing init for expcode %d: %s === \n',...
testCase.expcode_obj.maincode,...
testCase.expcode_obj.name);
testCase.expcode_obj.init; % initialize
end
function test_expcode_setup(testCase)
fprintf('\n=== Testing setup for expcode %d: %s === \n',...
testCase.expcode_obj.maincode,...
testCase.expcode_obj.name);
testCase.expcode_obj.setup; % run setup this exp code
end
function test_expcode_compile_nodes(testCase)
% compile each node separately first
nodes = testCase.expcode_obj.nodes;
for inode = 1:numel(nodes)
node = nodes{inode};
if isempty(node)
fprintf('skipping compilation for node %d since not configured\n',inode)
elseif ~node.active
fprintf('skipping compilation for node %d since not active\n',inode)
else
fprintf('\n === Testing Simulink compilation for node %02d of expcode %d: %s === \n',...
inode,testCase.expcode_obj.maincode,testCase.expcode_obj.name);
testCase.expcode_obj.compile(inode); % compile single node
end
end
end
function test_actualize(testCase)
testCase.expcode_obj.actualize(testCase.shot);
end
function test_expcode_sim(testCase)
testCase.assumeFail('Skipping all simulations tests temporarily to save time')
fprintf('\n === Testing Simulink simulation of tcv.slx for expcode %d: === \n',testCase.expcode_obj.maincode)
testCase.expcode_obj.sim; % simulate whole tcv.slx with this expcode
end
function test_build(testCase)
testCase.assumeFail('Skipping all build tests temporarily')
testCase.expcode_obj.build;
end
end
end
......@@ -4,7 +4,7 @@ classdef test_structbus < SCDtest
testBus = test_structbus.getTestBus();
end
methods(Test,TestTags={'Unit'})
methods(Test,TestTags={'unit'})
function testDifferentStruct(testCase)
mystruct = struct('a',1);
testCase.assertFalse(SCDconf_structbuscmp(mystruct,testCase.testBus));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment