Main Content

Use Test Sequence Scenarios in the Test Sequence Editor and Test Manager

This example shows how to create and use scenarios in the Test Sequence Editor. Scenarios let you include multiple test sequences in a Test Sequence block. If your test harness includes more than one Test Sequence block, you can move each test sequence to a scenario in a single Test Sequence block.

The example also shows how to use the Test Manager to set up and use iterations to run multiple scenarios in a single test case.

Open the Model and Test Harness

Open the HeatPumpScenario model, ScenarioTest harness, and Test Sequence Editor.

open_system('HeatPumpScenario')
sltest.harness.open('HeatPumpScenario/Controller','ScenarioTest');
open_system('ScenarioTest/Test Sequence')

Enable Scenarios

In the panel on the left side of the Test Sequence Editor, switch to the Scenarios tab and click Use Scenarios.

In the Start Using Scenarios dialog box, click OK, which confirms that when you switch to scenario mode you cannot revert the Test Sequence block to non-scenario mode. The existing steps and transitions are moved into a tab named Scenario_1.

Add a New Scenario

To add a new scenario, in the Scenarios tab, click the plus sign next to Scenario List. Alternatively, click on the plus sign next to the header of the Scenario_1 tab. The new scenario is named Scenario_2.

Duplicate a Scenario

To start a new scenario from an existing one, you can duplicate it.

Right-click the Scenario_1 tab label and select Duplicate Scenario_1 from the context menu. Alternatively, go to the Scenarios side panel, point to Scenario_1 in the Scenario List to display the Duplicate scenario button, and click it.

The Scenario List section updates and lists the new scenario, Scenario_3, which has the same content as Scenario_1.

Edit the Steps and Transitions

Modify Scenario_3 to change the transition of the Test_signals step to Troom_in >= 78.

Delete a Scenario

Right-click the Scenario_2 tab label and select Delete Scenario_2 from the context menu. Alternatively, in the left pane, in the Scenarios tab, point to Scenario_2 to display the Delete Scenario icon. Click the Delete Scenario icon and then click OK in the dialog box to delete Scenario_2.

The name of Scenario_3 does not change, but the scenario index shown to the left of the scenario name in the Scenario List changes to 2 because it is now the second scenario.

Activate Scenario

In the left pane, in the Scenario tab, a black lightning bolt icon and a bold Scenario name indicate that Scenario_1 is the currently active scenario. If you run the model, only the active scenario runs.

To change the active scenario to Scenario_3, right-click the Scenario_3 tab and select Activate Scenario_3 from the context menu. Alternatively, in the left pane, in the Scenarios tab, point to Scenario_3 to display the Click to Activate icon, which is a gray lightning bolt. Click the lightning bolt to make Scenario_3 the active scenario.

You can also control the active scenario from the command line. See the Programmatically Control the Active Scenario section below.

Run the Active Scenario

Right-click the Scenario_3 tab label and select Run Scenario_3 from the context menu. If you did not activate the scenario, the menu option is Activate and Run Scenario_3. Alternatively, run the model normally to run the active scenario. The Test Sequence Editor displays the active scenario during model simulation.

Programmatically Control the Active Scenario

You can alternatively use a variable in the base workspace, model workspace, or a data dictionary to programmatically control the active scenario. To activate a scenario, set the variable value to the index of the scenario.

1. In the Test Sequence Editor Scenarios tab, go to the Scenario Parameter section.

2. Enable Control active scenario from workspace. The previously active scenario deactivates. When you control the active scenario using a workspace variable, the Test Sequence Editor does not know which scenario is active until you click Run.

If you select Control active scenario from workspace, you cannot activate a scenario from the Test Sequence Editor using the right-click context menu or the Scenario List.

3. For this example, in the base workspace, create a variable named Active_Scenario_Index and set it to activate the first scenario, Scenario_1, by entering: Active_Scenario_Index = Simulink.Parameter(1);

4. Return to the Test Sequence Editor and click Run. Scenario_1 runs.

Instead of using Active_Scenario_Index as the name of the variable, you can specify a different name.

1. For this example, in the Test Sequence Editor, in the Scenario Parameter section, click on Active_Scenario_Index and enter a new name, such as ChangeScenario.

2. In the base workspace, create a variable named ChangeScenario. Set it to the desired scenario index, such as 1, by entering: ChangeScenario = Simulink.Parameter(1);.

3. Return to the Test Sequence Editor and click Run. Scenario_1 runs.

Use Iterations to Run Multiple Scenarios in a Test Case

You can run multiple scenarios in a test case by using iterations. This section describes scenarios in iterations using the Test Manager.

1. In the Test Manager, create a test file, test suite, and simulation test case.

2. In the System Under Test section, set the Model to HeatPumpScenario and the Harness to ScenarioTest.

3. In the Inputs section, click the Refresh icon next to the Test Sequence Block field to populate it with paths to Test Sequence blocks in the harness.

4. Set Test Sequence Block to the ScenarioTest/Test Sequence block, which has the scenarios to use in the iterations.

5. Click the Refresh icon next to the Override with Scenario field to populate it with the scenarios in the selected block.

6. Set Override with Scenario to Scenario_1, which set that scenario as the default for all iterations. This scenario overrides the active scenario in the Test Sequence block. In the Iterations section, you can change this default scenario to another scenario for each iteration.

If you do not select a scenario, the active scenario in the Test Sequence block is used as the default.

7. In the Iterations section, expand Table Iterations, click the plus sign in the upper right of the table and select Test Sequence Scenario to add that column to the table.

8. Click Add at the bottom of the Table Iterations to add the individual iterations. The Test Sequence Scenario column for each iteration shows the default scenario. In this case, it shows [Default] Scenario_1.

Alternatively, click Auto Generate and select Test Sequence Scenario to generate an iteration for each scenario in the selected Test Sequence block. In the Test Sequence Scenario column, each iteration is assigned a separate iteration.

9. In the Test Sequence Scenario column of an iteration, click the scenario name to view a list of available scenarios. Select a different scenario from the default for one of the iterations.

10. Run the test.

See Also

Related Topics