Main Content

Verify and Validate Variant Models with Code Compile Activation Time

This example shows how to verify and validate variant models with code compile variant activation time using Simulink® Design Verifier™ and Simulink Test™.

Overview of Simulink Design Verifier and Simulink Test

Simulink Design Verifier identifies hidden design errors and detects model constructs that result in integer overflow, dead logic, array access violations, and division by zero. It also generates test cases for model coverage. For more information, see About Systematic Model Verification Using Simulink Design Verifier (Simulink Design Verifier).

Simulink Test provides tools for authoring, managing, and executing systematic, simulation-based tests of models, generated code, and simulated or physical hardware. You can create nonintrusive test harnesses to isolate the component under test and use the Test Manager to manage and execute user-defined tests. For more information, see Functional Testing for Verification (Simulink Test).

Note: The variant model slexVariantVnVWorkflow used in this example uses the code compile variant activation time. For such models, Simulink Test analyzes only the active variant choice by default. You must switch the active variant choice and rerun the tests to analyze all choices. To make the Simulink Test workflow iterative, you can use the startup variant activation time and run the test iterations in fast restart. The startup variant with the fast restart workflow allows you to switch the active variant choice in each iteration without the need to recompile the model to change the active variant. For an example that shows the Simulink Test workflow with startup variants and fast restart, see Verify and Validate Variant Models with Startup Activation Time.

Explore the Model

Open the model, slexVariantVnVWorkflow.


The model contains a Variant Subsystem block, ComputeTargetSpeed, with two variant choices, ComputeTargetSpeed_Config1 and ComputeTargetSpeed_Config2. In this example, we use Simulink Design Verifier to generate test cases that satisfy condition and decision coverage objectives. The model uses a test harness, slexVariantVnVWorkflow_mdlHarness, created using Simulink Test and contains requirements based tests for each variant choice. The harness is saved with the model.

  • To launch Simulink Design Verifier and automatically generate test cases for the variant choices in the model, double-click the SLDV Run button.

  • To launch Simulink Test Manager and to view the test suites in the test harness, slexVariantVnVWorkflow_mdlHarness, double-click the Open Simulink Test Manager button.

Use Simulink Design Verifier to Generate Test Cases

Simulink Design Verifier analyzes only the active variant choice in a variant model by default. You can write a MATLAB® script to generate test cases for all variant choices.

To view the configuration parameters associated with Simulink Design Verifier for this model:

  • In the Simulink Editor, on the Modeling tab, click Model Settings.

  • In the Configuration Parameters dialog box, click Design Verifier in the left side pane. In the Analysis options section, Mode is set to Test Generation. In the Test Generation category, Model coverage objectives is set to Condition Decision.

In the MATLAB Command Window, run the script corresponding to each of these steps. The SLDV Run button in the model automates the same script.

Step 1: Set the required Simulink Design Verifier options. For information on the options, see sldvoptions (Simulink Design Verifier).

%Create a design verification options object for the model.
opts = sldvoptions(modelName);
%Specify the optimization strategy to use when generating test cases.
opts.TestSuiteOptimization = 'Auto';
%Generate and save a Simulink Design Verifier report.
%Create unique reports for each variant choice.
opts.MakeOutputFilesUnique ='on';
%Create a harness model generated by the Simulink Design Verifier analysis.
opts.SaveHarnessModel ='off';
%Specify the analysis mode.
opts.Mode = 'TestGeneration';

Step 2: Set the variant choice for which tests need to be generated, then analyze the model.

%Set the active variant choice to ComputeTargetSpeed_Config1 in the base workspace.
%Specify a file name for the analysis report to indicate the variant choice being analyzed.
opts.ReportFileName = ([modelName 'report_speedConfig1']);
%Track the execution progress from MATLAB command prompt.
disp([newline 'Performing SLDV Run for Model ' modelName ' with variant choice : SpeedConfig 1']);
%Invoke the model update command to activate the variant choice.
set_param(modelName, 'SimulationCommand', 'Update');
%Analyze the model to generate test cases and display messages in the log window.
[status,fileNames] = sldvrun(modelName, opts, true);

Step 3: Repeat Step 2 for the variant choice ComputeTargetSpeed_Config2.

opts.ReportFileName = ([modelName 'report_speedConfig2']);
disp([newline 'Performing SLDV Run for Model ' modelName ' with variant choice : SpeedConfig 2']);
set_param(modelName, 'SimulationCommand', 'Update');
[status1,fileNames] = sldvrun(modelName, opts, true);

Step 4: Display the test generation status.

if status & status1
disp('Simulink Design Verifier test generation completed.');

After the analysis is complete, the Simulink Design Verifier Results Summary window opens and shows different ways you can use the results. See Review Analysis Results (Simulink Design Verifier). To export the generated tests to Simulink Test, click Export test cases to Simulink Test.

Use Simulink Test to Execute Tests

You can use Simulink Test to execute user-defined tests or tests imported from Simulink Design Verifier. To execute the tests and analyze the results, use the Test Manager. Simulink Test analyzes only the active variant choice by default for variant models that use the code compile variant activation time. To analyze all variant choices in such models:

1. On the Apps tab, in the Model Verification, Validation, and Test section, click Simulink Test.

2. On the Tests tab, click Simulink Test Manager.

3. Create a test harness with the required test cases and select the harness model in the Test Manager.

4. Add the variant control variables to a parameter set in the Parameter Overrides section.

5. For each test case, change the value of the variant control variables in the parameter set according to each variant choice, then re-run the tests.

Note: If your model has variant configurations defined using Variant Manager for Simulink, you can use those configurations when running programmatic tests for both test cases and test iterations (since R2024a). With this workflow, you do not need to create parameter sets to re-define the variant control variable values to use with the test cases or test iterations. For an example, see Run Tests for Variant Models Using Variant Configurations.

Explore the test harness model

To see the test suites in the harness model, slexVariantVnVWorkflow_mdlHarness, double-click the Open Simulink Test Manager button in the model.

  • In the test suite CruiseControl_SpeedConfig_1, the active variant choice is set to ComputeTargetSpeed_Config1. In CruiseControl_SpeedConfig_2, the active choice is set to ComputeTargetSpeed_Config2.

  • Select a test suite and click Run in the Simulink toolstrip to run the tests. When the test finishes, click the Results and Artifacts tab in the Test Manager to review the aggregated results and coverage metrics. See Perform Functional Testing and Analyze Test Coverage (Simulink Test).

See Also