Main Content

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

  • Fault sets

  • Sequence diagrams (since R2025a)

Set up iterations in the Iterations section of a test case. You can create iterations by using the table in the Iterations section of the Test Manager or by using a script.

To use Test Sequence scenarios in iterations, in the Inputs section, set Test Sequence Block to the block that contains the scenarios. Then, to use a scenario as the default for each iteration, select the scenario from Override with Scenario. If you don't select a scenario, the Test Manager uses the active scenario in the Test Sequence block. 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) simulation modes, 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 doubles, 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

Use the Table Iterations section to add iterations based on items in your model or test case. To create iterations with the table, first make the appropriate columns visible:

  1. Expand the Iterations > Table Iterations section.

  2. In the table, add or remove columns by clicking the Add column button and selecting items in the list. For example, to select display parameter and configuration sets, select the Parameter Set and Configuration Set items.

    Table iterations column selection options

Add Iterations Manually

  1. To manually add iterations, click Add. The table displays a new iteration row.

  2. Enter an iteration name in the Name field 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.

    Table of iterations

Generate Table Iterations

You can also automatically generate iterations from data in your test case and model.

  1. Click the Auto Generate button.

  2. Select items for which to generate iterations.

    For Test Sequence scenarios, the Test Manager generates an iteration for each scenario in the block you select in the Test Sequence Block in the Inputs section.

    If you select multiple items, the Test Manager creates iterations in sequential pairings. For example:

    • The model sldemo_autotrans has a Signal Editor block with four signal scenarios, named Coasting, Gradual_Acceleration, Hard_braking, and Passing_Maneuver, each of which has Throttle and Brake signals. To open this model, type openExample("sldemo_autotrans") at the command line. To view the Signal Editor, double-click the ManeuversGUI block to open the Block Parameters dialog box. Then, click the Open Signal Editor button.

    • 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 appear in the Signal Editor block and parameter set section.

    Signal Editor with four scenarios and plot of passing maneuver throttle.

    Parameter overrides and resulting iterations

  3. 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 OptionPurpose

Signal Editor Scenario

Applies to the Inputs section of a simulation, baseline, or equivalence test case, for the specified Signal Editor Scenario. The Test Manager uses each Signal Editor scenario to generate an iteration.

Parameter Set

Applies to the Parameter Overrides section of a simulation, baseline, or equivalence test case. The Test Manager uses each parameter override set 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. The Test Manager uses each external input set 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. The Test Manager uses each logged signal set to generate an iteration.

Baseline

Applies only to the Baseline Criteria section of a baseline test case. The Test Manager uses each baseline criteria set 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. The Test Manager each Test Sequence scenario to generate an iteration.

Fault Set

Applies to the Fault Settings section of a simulation or baseline test case. The Test Manager uses each fault set to generate an iteration.

Sequence Diagrams

Applies to the Sequence Diagram Assessment section of a simulation or baseline test case. The Test Manager uses each sequence diagram to generate an iteration.

Simulation Modes

Applies to the Iterations table section of a simulation or baseline test case. The Test Manager creates an iteration for the current simulation mode of the model and a SIL iteration 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, select Simulation 1 or Simulation 2. The simulation you select in the Auto Generate Iterations dialog box corresponds to the two simulation sections within the equivalence test case.

Create Scripted Iterations

You can run a custom set of iterations by using a script in the Scripted Iterations section. For example, you can define parameter sets or customize iteration order. The Test Manager generates the iterations at run time when a test executes and run before loading the model.

Code for scripted iterations

Iteration Script Components

An iteration script must contain certain components. A script must contain these elements:

  1. An iteration object created using sltestiteration

  2. An iteration setting set using setTestParam

  3. 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. For example:

%% 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 in 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 string

  • sltest_sut — The System Under Test, defined as a string

  • sltest_isharnesstrue if sltest_bdroot is a harness model, defined as a logical

  • sltest_externalInputs — Name of external inputs, defined as a cell array of strings

  • sltest_parameterSets — Name of parameter override sets, defined as a cell array of strings

  • sltest_configSets — Name of configuration settings, defined as a cell array of strings

  • sltest_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_sequenceDiagrams — Name of sequence diagrams, defined as a cell array of character vectors.

  • 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 of sltest.testmanager.TestIteration objects

  • sltest_testCase — Current test case object, defined as an sltest.testmanager.TestCase object

Scripted Iteration Templates

If you are using a baseline test case, you can quickly generate iterations for your test case by using templates for Signal Editor scenarios, parameter sets, external inputs, configuration sets, and baseline sets. 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:

  1. Click Iteration Templates.

  2. Select the test case settings you want to iterate through. Click OK.

    The Test Manager generates the script and adds it to the script box below any existing scripts.

  3. To generate a table that previews 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.

Details of iterations, including parameter names and values, and workspace and model parameter overrides

7. Click Run to run the test and generate a result for each iteration.

Iteration results hierarchy

8. Clear and close the Test Manager

sltest.testmanager.clear
sltest.testmanager.clearResults
sltest.testmanager.close

See Also

|

Topics