Modifying variables in properties (TestParameter) of the unittest class
조회 수: 6 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Steven Lord
2021년 4월 7일
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
Steven Lord
2021년 4월 7일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Testing Frameworks에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!