- Testing Frameworks - MATLAB & Simulink (mathworks.com)
- Simulink Test Documentation (mathworks.com)
- Creates SimulationInput objects to make changes to model for multiple or individual simulations - MATLAB (mathworks.com)
- Access simulation outputs and metadata - MATLAB (mathworks.com)
Class-Based Unit Tests in MATLAB: timeseries function crash against a data type String as constant?
1 次查看(过去 30 天)
显示 更早的评论
Hello Community.
From the first look it seems to be a simple question. But....
Situation: Simulink model. I have Subsystem only with one "Constant"-Block with Data Type "String", for example '011101111' . One output block with the signal name "Signal_output_name". Nothing else.
I am writing simple test case as m-File using class <matlab.unittest.TestCase
The Code is included and attached.
Problem place in code
----------------------------------------------
%% Outputs:
Signal_output_name = ['011101111' '011101111' '011101111' '011101111' '011101111' '011101111' '011101111']';
%% Compare the results
testCase.simIn=testCase.simIn.setExternalInput(inputData); %Set the externatl input values
simOut = sim(testCase.simIn); %get the outputs
testCase.assertEqual(simOut.yout{1}.Values.Data,Signal_output_name); %check the values
------------------------------
If i run the code it doesn´t work, because of:
- MATLAB uses in this case the function "timeseries()" and it doesn´t work with data type "String"
Question: How can i make unit test?
Thanks a lot
0 个评论
回答(1 个)
Karan Singh
2023-9-6
Hi Vladlen,
From what I understand, you have an issue with MATLAB's “timeseries” function, which does not support string data types. Therefore, the unit test fails when comparing the string array to the output data.
The solution is to convert the string array “Signal_output_name” and the output data to character arrays using the “char” function before performing the comparison. This ensures compatibility and allows for successful unit testing. Here is an updated version of your code with the necessary modifications:
classdef SolverTest < matlab.unittest.TestCase
properties
simIn
mdlStartTime = '0';
mdlStopTime = '0.006';
mdlStepSize = '0.001'
loc = ''
end
methods (TestMethodSetup)
function addSimulationInput(testCase)
testCase.simIn = Simulink.SimulationInput('name_of_the_subsystem');
end
function setDefaultValuesForModelParams(testCase)
% Please adjust the values according to your test suite
testCase.mdlStartTime = '0';
testCase.mdlStopTime = '0.006';
testCase.mdlStepSize = '0.001';
end
function setSimulationParameter(testCase)
testCase.simIn = testCase.simIn.setModelParameter('StartTime', testCase.mdlStartTime, 'StopTime', testCase.mdlStopTime); % Set the simulation time
testCase.simIn = testCase.simIn.setModelParameter('SolverType', 'FixedStep');
testCase.simIn = testCase.simIn.setModelParameter('FixedStep', testCase.mdlStepSize);
testCase.simIn = testCase.simIn.applyToModel; % Apply the changes to the model
end
end
methods (Test)
function testStringData(testCase)
testCase.mdlStopTime = '0.006'; % Increase stoptime to be able to test more cases
setSimulationParameter(testCase); % Set stop time to 0.015
inputData = Simulink.SimulationData.Dataset;
inputData.Name = 'testStringData';
% Outputs:
Signal_output_name = ['011101111'; '011101111'; '011101111'; '011101111'; '011101111'; '011101111'; '011101111'];
% Compare the results
testCase.simIn = testCase.simIn.setExternalInput(inputData); % Set the external input values
simOut = sim(testCase.simIn); % Get the outputs
% Convert string data to character array for comparison
expectedData = char(Signal_output_name);
actualData = char(simOut.yout{1}.Values.Data);
testCase.assertEqual(actualData, expectedData); % Check the values
end
end
end
In this updated code, the string data “Signal_output_name” is converted to a character array using the “char” function before comparing it with the simulation output data. Similarly, the simulation output data is also converted to a character array for comparison.
Attached below are some documentation links that you may find helpful:
Hope this helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Inputs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!