Share values that are created upon running a suite of pramatized unit tests in parallel
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hello,
I need help with sharing values that are created upon running a suite of pramatized unit tests in parallel.
I'm guessing this can be achieved with parallel plugins or shared fixtures but I'm not sure on how to use either of them.
Below is an example taken from Create Basic Parameterized Test - MATLAB & Simulink - MathWorks United Kingdom and edited to try and show what I am trying to achieve, showing the diffference of running sequentially and in parallel.
Any help would be greatly appreciated.
Regards
Imraan
runtests("TestCarpet")
delete(TestCarpet.FileName)
Name Passed Failed Incomplete Duration Details
__________________________________________________ ______ ______ __________ _________ ____________
{'TestCarpet/testNumel(level=small,side=small)' } true false false 0.015111 {1×1 struct}
{'TestCarpet/testNumel(level=medium,side=medium)'} true false false 0.0058167 {1×1 struct}
{'TestCarpet/testNumel(level=large,side=large)' } true false false 0.0078009 {1×1 struct}
runtests("TestCarpet","UseParallel",true)
delete(TestCarpet.FileName)
Name Passed Failed Incomplete Duration Details
__________________________________________________ ______ ______ __________ ________ ____________
{'TestCarpet/testNumel(level=small,side=small)' } true false false 0.085126 {1×1 struct}
{'TestCarpet/testNumel(level=medium,side=medium)'} false true false 0.68034 {1×1 struct}
{'TestCarpet/testNumel(level=large,side=large)' } false true false 0.67238 {1×1 struct}
Example taken from Create Basic Parameterized Test - MATLAB & Simulink - MathWorks United Kingdom and edited to implement my example.
classdef TestCarpet < matlab.unittest.TestCase
properties(Constant)
FileName = "somedata.txt";
end
properties
% Values I want to share in parallel
Date (1,:) datetime
UUID (1,:) string
end
properties (TestParameter)
type = {'single','double','uint16'};
level = struct('small',2,'medium',4,'large',6);
side = struct('small',9,'medium',81,'large',729);
end
methods(TestClassSetup)
function init(this)
% Values I want to share in parallel.
this.Date = datetime('now');
this.UUID = matlab.lang.internal.uuid;
if ~isfile(this.FileName)
lines = [string(this.Date); this.UUID];
writelines(lines, this.FileName);
end
end
end
methods (Test, ParameterCombination = 'sequential')
function testNumel(testCase,level,side)
import matlab.unittest.constraints.HasElementCount
% ----- I want the date and UUID common for all Parameterized tests when running in parallel
expectedData = readlines(testCase.FileName, 'EmptyLineRule','skip');
actualData = [string(testCase.Date); testCase.UUID];
arrayfun(@(x,y) testCase.verifyMatches(x, y), actualData, expectedData)
%---------------------------------------------------------
% Code from Matlab Documentation that I don't care about
% testCase.verifyThat(sierpinski(level), HasElementCount(side^2))
end
end
end
3 Commenti
Alvaro
il 3 Gen 2023
Would parameterization properties work for you? For example:
classdef myTestClass < matlab.unittest.TestCase
properties(Constant)
FileName = "somedata.txt";
end
properties(TestParameter)
myParam = {datetime('now')};
anotherParam = {matlab.lang.internal.uuid};
end
methods(Test)
function myTest1(testcase, myParam, anotherParam)
writelines(string(myParam), testcase.FileName, WriteMode = "append");
writelines(string(anotherParam), testcase.FileName, WriteMode = "append");
end
function myTest2(testcase, myParam, anotherParam)
writelines(string(myParam), testcase.FileName, WriteMode = "append");
writelines(string(anotherParam), testcase.FileName, WriteMode = "append");
end
function myTest3(testcase, myParam, anotherParam)
writelines(string(myParam), testcase.FileName, WriteMode = "append");
writelines(string(anotherParam), testcase.FileName, WriteMode = "append");
end
end
end
Try runtests("myTestClass",'UseParallel',true) and you will see in somedata.txt that all workers have access to same timestamp and UUID.
Risposte (0)
Vedere anche
Categorie
Scopri di più su Run Unit Tests in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!