Filter löschen
Filter löschen

Share values that are created upon running a suite of pramatized unit tests in parallel

2 Ansichten (letzte 30 Tage)
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 Kommentare
Imraan Ibrahim
Imraan Ibrahim am 3 Jan. 2023
Hi,
Yes, I am trying to initialize a parallel pool of workers and have each worker have access to the same timestamp and uuid.
This works when I'm NOT running in parallel as the TestClassSetup runs once for all the parmatized unit tests.
As you mentioned, the TestClassSetup method will run locally on each worker, which means that each call of TestClassSetup will give slightly different timestamps the workers don't start at the exact same time and also the uuids will be different.
Constant properties won't work for me as I need a new Timestamp and uuid each time I run a test suite.
Alvaro
Alvaro am 3 Jan. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Run Unit Tests finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by