Modifying variables in properties (TestParameter) of the unittest class

10 visualizaciones (últimos 30 días)
Akshay Jajoo
Akshay Jajoo el 7 de Abr. de 2021
Editada: Akshay Jajoo el 15 de Jul. de 2021
My unit testcase inputs are very large in size (There are structs which have many fields. So, too many characters). This is making the testcase inputs look very ugly and also difficult to read, update etc.
properties(TestParameter)
TRU = {struct('clusters', [], 'freq_links', [1,2], 'SC', 1,'hC', [struct('lin', cat(3, [1, 2; 3, 4], [5, 4; 6, 5], [7, 3; 2, 3], [])), ...
struct('lin', cat(3, [1, 2; 3, 4], [5, 4; 6, 5], [7, 3; 2, 3], []))]), ...
struct('clusters', [], 'freq_links', [1,2],'SC', 1, 'hC', [struct('lin', cat(3, [1+2i, 2; 3, 4+1i], [5, 4+5i; 6, 5i], [7i, 3i; 2, 3], [])), ...
struct('lin', cat(3, [1, 2; 3, 4], [5, 4; 6, 5], [7, 3; 2, 3], []))]), ...
struct('clusters', [2, 3, 4], 'freq_links', [1,2],'SC', 1, 'hC', [struct('lin', cat(3, [1, 1; 2, 2], [1+2i, 2; 3, 4+1i], [5, 4+5i; 6, 5i], [7i, 3i; 2, 3], [])), ...
.
.
};
.
.
end
I tried making these inputs concise and more readable by several means:
  • Declaraing other small variables like a as shown below in the same property block as well in an new property block. Then use a in creating the elements of TRU (the test parameter);
a = [1, 2; 3, 4]
  • Modifying the TRU and the other test parameters defining methods in a general section or in methods(TestClassSetup).
I have also tried several other variants of the other two approaches, however, no success. I get errors similar to the following;
Error using matlab.unittest.internal.TestSuiteFactory/createSuiteFromParentName (line 29)
Cannot specify a get function for property 'a' in class 'UL_compute_flat_interf_est_Test', because that property is
not defined by that class.
Error in testsuite>createSuite (line 180)
suite = factory.createSuiteFromParentName(selector);
Error in testsuite (line 130)
suites{k} = createSuite(testAsChar, selector, ...
Error in runtests (line 98)
suites = testsuite(parseResults.TestsuiteInputs{:});
Any suggestions on how can I write the TestParameters in modular and cleaner way? Please feel free to get back if you have any questions.

Respuestas (1)

Steven Lord
Steven Lord el 7 de Abr. de 2021
You can define a class-related function at the end of the file, after the end that matches the classdef keyword, that returns the parameters.
classdef myTest < matlab.unittest.TestCase
properties(TestParameter)
TRU = generateTRUParameters();
end
end
function TRU = generateTRUParameters()
lin = cat(3, [1, 2; 3, 4], [5, 4; 6, 5], [7, 3; 2, 3], []);
TRU.case1 = struct('clusters', [], 'freq_links', [1,2], 'SC', 1,'hC', 'lin', lin);
% etc.
end
I would probably define TRU not as a cell array like you did but as a struct array. I used this approach in the example above. But in a real test I would use a more descriptive name than case1.
Writing this as a function lets you define local variables to simplify the expressions of your test parameters, but it also lets you apply the "out of sight, out of mind" pattern. If you need to know exactly how the parameters are defined you can look at the end of the file but in most circumstances you can forget about that function.
  2 comentarios
Akshay Jajoo
Akshay Jajoo el 7 de Abr. de 2021
Thanks for the response @Steven Lord. If I won't define it as a cell array how will the
testcases paramaterization feature work?
I tried the above example with creating TRU.case1, TRU.case2. But my 'sequential' test parametrization was failing.
Steven Lord
Steven Lord el 7 de Abr. de 2021
Can you show a small sample test where the parameterization (with the property containing a scalar struct array) fails? Use the level and side test parameters from the examples on this documentation page as a model.
Now that I think about it, if you're using release R2021a or later the TestParameterDefinition attribute may also be of interest to you. See the Release Notes for more information.

Iniciar sesión para comentar.

Categorías

Más información sobre Write Unit Tests en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by