Test Iterations
You can run the same test case with different data or configuration sets by using test case iterations. Iterations can use different:
Parameters
External inputs
Configuration sets
Variant configurations
Signal Editor scenarios
Test Sequence scenarios
Baseline data
Simulation modes
Set up iterations in the Iterations section of a test case. You can create iterations using the table in the Iterations section of the Test Manager or by using a script.
To use Test Sequence scenarios in iterations, first, in the Inputs section, set Test Sequence Block to the block that contains the scenarios. Then, select a scenario from Override with Scenario to use that scenario as the default for each iteration. If you don't select a scenario, the active scenario in the Test Sequence block is used as the default. Use the Test Sequence Scenario column in the table to change the scenario for an iteration. For more information, see Use Test Sequence Scenarios in the Test Sequence Editor and Test Manager.
To use different simulation modes, such as normal and software-in-the-loop (SIL), for a baseline or simulation test, first, set up the test case. Then, in the Iterations table, click Auto Generate. In the Auto Generate Iterations dialog box, select Simulation Modes and one or more other options. For each option, the number of iterations created is doubled, one for the mode of the model and one for SIL mode.
If the test collects coverage using Simulink® Coverage™, the same coverage settings apply to all iterations in the test case.
Whether you use table or scripted iterations, you can see the iterations in the test case by clicking the Show Iterations button.
Create Table Iterations
Table Iterations provide a quick way to add iterations based items in your model or test case. To create iterations with the table, first make the appropriate columns visible:
Expand the Iterations > Table Iterations section.
In the table, add or remove columns by clicking the button and selecting items in the list. For example, to select display parameter and configuration sets, select the Parameter Set and Configuration Set items.
Add Iterations Manually
To manually add iterations, click Add. The table displays a new iteration row.
Assign an iteration name and select items for the iteration. For example, this test case has four iterations. Each iteration uses a different combination of external input and baseline data.
Generate Table Iterations
You can also automatically generate iterations from data in your test case and model:
Click the Auto Generate button.
Select items for which to generate iterations.
For Test Sequence scenarios, an iteration is generated for each scenario for the block you selected in the Test Sequence Block in the Inputs section.
If you select multiple items, iterations are created in sequential pairings. For example:
The model
sldemo_autotrans
has a Signal Editor block with four signal scenarios, labeled Coasting, Gradual_Acceleration, Hard_braking, and Passing_Maneuver, each of which has Throttle and Brake signals. To open this model, typeopenExample('sldemo_autotrans')
at the command line. To view the Signal Editor, double-click the ManeuversGUI block to open the Block Parameters dialog. Then, click the Signal Editor launch button under Signal Properties.The test case has three parameter sets, labeled P1, P2, and P3.
Automatically generating iterations from Signal Editor scenarios and parameter sets results in three iterations. The iterations are limited by the three parameter sets. Each iteration contains one Signal Editor scenario and one parameter set. The Signal Editor scenario and parameter set are matched in the order that they are listed in the Signal Editor block and parameter set section.
Specify an optional naming rule for the iterations. In the Iteration naming rule box, enter the rule using:
The name of each setting you want to use in the name, with spaces removed
An underscore or space to separate each setting
For example, if you want to include the name of the parameter set, configuration set, and baseline filename, enter
ParameterSet_ConfigurationSet_Baseline
.
Section Option | Purpose |
---|---|
Signal Editor Scenario | Applies to the Inputs section of a simulation, baseline, or equivalence test case, for the specified Signal Editor Scenario. Each Signal Editor scenario is used to generate an iteration. |
Parameter Set | Applies to the Parameter Overrides section of a simulation, baseline, or equivalence test case. Each parameter override set is used to generate an iteration. |
External Input | Applies to the Inputs section of a simulation, baseline, or equivalence test case, for the specified External Inputs sets. Each external input set is used to generate an iteration. |
Configuration Set | Applies to the Configuration Settings Overrides section of a simulation, baseline, or equivalence test case. Each iteration uses the specified configuration setting. |
Variant Configuration | Applies to the Parameter Overrides section of a simulation, baseline, or equivalence test case. Each iteration uses the specified variant configuration. |
Logged Signal Set | Applies to the Logging section of a simulation, baseline, or equivalence test case. Each logged signal set is used to generate an iteration. |
Baseline | Applies only to baseline test case types, specifically to the Baseline Criteria section of a baseline test case. Each baseline criteria set is used to generate an iteration. |
Test Sequence scenario | Applies to the Inputs section of a simulation, baseline, or equivalence test case, for the specified Test Sequence Block. Each Test Sequence scenario is used to generate an iteration. |
Simulation Modes | Applies to the Iterations table section of a simulation or baseline test case. A iteration for the current simulation mode of the model and a SIL iteration are created for each other test setting selected in the Auto Generate Iterations dialog box. |
Simulation 1 or 2 | Applies only to equivalence test case types. At the top of the Auto Generate Iterations dialog box, there is a menu for Simulation 1 or Simulation 2. These sections correspond to the two simulation sections within the equivalence test case. |
Create Scripted Iterations
You can run a custom set of iterations using a script in the Scripted Iterations section. For example, you can define parameter sets or customize iteration order by using a custom iteration. Scripted iterations are generated at run time when a test executes and run before loading the model.
Iteration Script Components
An iteration script must contain certain components. The most basic iteration script contains three elements:
An iteration object, created using
sltestiteration
.An iteration setting, set using
setTestParam
.The iteration registration, added using
addIteration
.
For example, this script creates an iteration that runs one signal scenario from a Signal Editor block.
%% Iterate Using a Signal Editor Scenario % Set up a new iteration object testItr = sltestiteration; % Set iteration setting using Signal Editor scenario setTestParam(testItr,'SignalEditorScenario',... sltest_signalEditorScenarios{1}); % Add the iteration to run in this test case % The predefined sltest_testCase variable is used here addIteration(sltest_testCase,testItr);
For more information about the test iteration class, see sltest.testmanager.TestIteration
.
You can iterate over multiple items, such as Signal Editor or
Test Sequence scenarios. You can iterate over all
Signal Editor or Test Sequence scenarios in the
block by putting the basic iteration script in a loop:
%% Iterate Over All Signal Editor Scenarios % Determine the number of possible iterations numSteps = length(sltest_signalEditorScenarios); % Create each iteration for k = 1 : numSteps % Set up a new iteration object testItr = sltestiteration; % Set iteration settings setTestParam(testItr,'SignalEditorScenario',... sltest_signalEditorScenarios{k}); % Add the iteration to run in this test case % You can pass in an optional iteration name addIteration(sltest_testCase,testItr); end
Predefined Variables
You can use predefined variables to write iterations scripts. To see the list of predefined variables in the Test Manager, expand the Help on creating test iterations section. You write the iterations script in the script box within the Scripted Iterations section. The script box is a functional workspace, which means the MATLAB® base workspace cannot access information from the script box. If you define variables in the script box, then other workspaces cannot use the variable.
The predefined variables are:
sltest_bdroot
— Model simulated by the test case, defined as a stringsltest_sut
— The System Under Test, defined as a stringsltest_isharness
—true
ifsltest_bdroot
is a harness model, defined as a logicalsltest_externalInputs
— Name of external inputs, defined as a cell array of stringssltest_parameterSets
— Name of parameter override sets, defined as a cell array of stringssltest_configSets
— Name of configuration settings, defined as a cell array of stringssltest_signalEditorScenarios
— Name of Signal Editor scenarios, defined as a 2-D cell array of character vectors.sltest_loggedSignalSets
— Name of logged signal sets, defined as a 2-D cell array of character vectors.sltest_testSequenceScenarios
— Name of test sequence scenarios, defined as a 2-D cell array of character vectors.sltest_faultSets
— Name of fault sets, defined as a 2-D cell array of character vectors. You must have Simulink Fault Analyzer™ to use this predefined variable.sltest_baselines
— Name of baseline criteria, defined as a cell array of character vectors, for baseline tests only.sltest_tableIterations
— Iteration objects created in the iterations table, defined as a cell array ofsltest.testmanager.TestIteration
objectssltest_testCase
— Current test case object, defined as ansltest.testmanager.TestCase
object
Scripted Iteration Templates
You can quickly generate iterations for your test case using templates for Signal Editor scenarios, parameter sets, external inputs, configuration sets, and baseline sets, if you are using a baseline test case. Scripted iteration templates follow lockstep ordering and pairing of test settings. For more information about lockstep ordering, see Create Table Iterations.
For example, if you want to run all Signal Editor scenarios in a scripted iteration:
Click Iteration Templates.
Select the test case settings you want to iterate through. Click OK.
The script is generated and added to the script box below any existing scripts.
To generate a table that gives a preview of the iterations that execute when you run the test case, click Show Iterations.
Sweep Through a Set of Parameters
This example shows how to use scripted iterations to test a model by sweeping through a set of parameters. You can use this iteration script to try different values for the model workspace parameter Iei
and model parameter UpperSaturationLimit
in the model sf_car
.
1. Open the model.
sf_car
2. Open the Test Manager.
sltest.testmanager.view
3. Use New > Test File to create a new test file, which contains a default test suite and test case.
4. Select New Test Case 1 and enter sf_car
in the Model field of the System Under Test section.
5. Add this script in the Iterations > Scripted Iterations section.
%% Iterate over Iei parameter
% Set up the parameter values to sweep over
IeiValues = [0.021,0.022,0.022,0.023];
UprSatValues = [2000,3000,4000,5000];
numSteps = length(IeiValues);
% Create each iteration
for k = 1 : numSteps
testItr = sltestiteration;
setVariable(testItr,'Name','Iei','Source','model workspace', ...
'Value',IeiValues(k));
testItr.setModelParam('sf_car/Engine/Integrator', ...
'UpperSaturationLimit',string(UprSatValues(k)));
addIteration(sltest_testCase,testItr);
end
6. After you add the script, click Show Iterations. You can see the iterations that the script created.
7. Click Run to run the test and generate a result for each iteration.
8. Clear and close the Test Manager
sltest.testmanager.clear sltest.testmanager.clearResults sltest.testmanager.close
See Also
sltest.testmanager.TestIteration
| setModelParam