使用信号编辑器输入模块仿真框架模型
此示例展示如何通过使用信号编辑器输入模块仿真测试框架模型来生成模型覆盖率报告。您可以通过在信号编辑器对话框中选择活动场景来仿真单个测试用例或反例。有关更多信息,请参阅使用信号编辑器输入模块仿真框架模型。
要仿真所有测试用例并测量它们的组合模型覆盖率,请使用cvsim
或parsim
命令。
在此示例中,您可以通过选择信号编辑器作为框架源来生成框架模型。信号编辑器场景由与测试用例或反例相关的信号源组成。然后,为了生成组合模型覆盖率报告,您可以使用 cvsim
或 parsim
函数仿真所有场景。
1.打开模型并配置框架选项
使用 sldvharnessopts
选项为 sldvdemo_cruise_control
模型创建一个框架模型。将 HarnessSource
选项设置为 Signal Editor
。
model = 'sldvdemo_cruise_control'; open_system(model); opts = sldvoptions; opts.Mode = 'TestGeneration'; opts.SaveHarnessModel = 'on'; opts.HarnessSource = 'Signal Editor'; opts.HarnessModelFileName = 'sldvdemo_cruise_control_harness'; opts.SaveReport = 'off';
2.生成测试用例
使用 sldvrun
函数和 sldvoptions
分析模型。
sldvrun('sldvdemo_cruise_control', opts); save_system('sldvdemo_cruise_control_harness');
17-Oct-2023 14:45:30 Checking compatibility for test generation: model 'sldvdemo_cruise_control' Compiling model...done Building model representation...done 17-Oct-2023 14:45:32 'sldvdemo_cruise_control' is compatible for test generation with Simulink Design Verifier. Generating tests using model representation from 17-Oct-2023 14:45:32... ........................ 17-Oct-2023 14:46:01 Completed normally. Generating output files: Harness model: C:\Users\vshukla\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\vshukla.Bdoc24a.j2397882\sldv-ex99648832\sldv_output\sldvdemo_cruise_control\sldvdemo_cruise_control_harness.slx 17-Oct-2023 14:46:09 Results generation completed. Data file: C:\Users\vshukla\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\vshukla.Bdoc24a.j2397882\sldv-ex99648832\sldv_output\sldvdemo_cruise_control\sldvdemo_cruise_control_sldvdata.mat
3.生成组合模型覆盖率报告
Simulink Design Verifier 在多重仿真模式下自动配置信号编辑器框架。为了仿真生成的测试用例并收集测试单元的覆盖率,点击仿真工具栏菜单上的运行全部(覆盖率)按钮。
或者,在分析生成框架模型后,您可以使用使用 cvtest
和 cvsim
函数的此代码来生成组合模型覆盖率报告。
signalEditorBlock = 'sldvdemo_cruise_control_harness/Inputs'; numOfScenarios = str2double(get_param(signalEditorBlock,'NumberOfScenarios')); harnessModel = 'sldvdemo_cruise_control_harness'; test = cvtest(harnessModel); test.modelRefSettings.enable = 'On'; test.modelRefSettings.excludeTopModel = 1; covData = []; for id = 1:numOfScenarios set_param(signalEditorBlock,'ActiveScenario',id); aCovData = cvsim(harnessModel); if isempty(covData) covData = aCovData; else covData = covData + aCovData; end end save_system('sldvdemo_cruise_control_harness'); cvhtml('Coverage_Harness',covData);
或者,您可以使用使用 parsim
函数生成组合模型覆盖率报告的代码。
signalEditorBlock = 'sldvdemo_cruise_control_harness/Inputs'; numOfScenarios = str2double(get_param(signalEditorBlock,'NumberOfScenarios')); harnessModel = 'sldvdemo_cruise_control_harness'; simIn = Simulink.SimulationInput.empty(0,numOfScenarios); for id = 1:numOfScenarios simIn(id) = Simulink.SimulationInput(harnessModel); simIn(id) = simIn(id).setBlockParameter(signalEditorBlock,'ActiveScenario', id); simIn(id) = simIn(id).setModelParameter('CovEnable', 'on'); simIn(id) = simIn(id).setModelParameter('CovSaveSingleToWorkspaceVar', 'on'); end simOut = parsim(simIn); cvhtml('Coverage_Harness',simOut.covdata);
[17-Oct-2023 14:46:17] Checking for availability of parallel pool... Starting parallel pool (parpool) using the 'Processes' profile ... Connected to parallel pool with 4 workers. [17-Oct-2023 14:47:09] Starting Simulink on parallel workers... [17-Oct-2023 14:47:45] Configuring simulation cache folder on parallel workers... [17-Oct-2023 14:47:45] Loading model on parallel workers... [17-Oct-2023 14:48:04] Running simulations... [17-Oct-2023 14:48:25] Completed 1 of 3 simulation runs [17-Oct-2023 14:48:25] Completed 2 of 3 simulation runs [17-Oct-2023 14:48:25] Completed 3 of 3 simulation runs [17-Oct-2023 14:48:26] Cleaning up parallel workers...
覆盖率报告表明,通过仿真 sldvdemo_cruise_control_model
的所有测试用例,实现了 100% 的覆盖率。
5.清理
% To complete this example, close the models. close_system('sldvdemo_cruise_control_harness', 0); close_system('sldvdemo_cruise_control', 0);