主要内容

代码覆盖率测试生成

此示例说明如何使用 Simulink® Design Verifier™ 生成测试用例以获得完整代码覆盖率。

首先,您需要收集配置为软件在环 (SIL) 仿真模式的示例模型的代码覆盖率。然后,使用 Simulink® Design Verifier™ 创建用于生成测试用例的测试套件。最后,在 SIL 仿真模式下执行生成的测试用例,以验证完整覆盖率。

检查产品可用性

确保您的计算机上安装了 Simulink® Coder™ 和 Embedded Coder™ 软件。

if ~(license('test', 'Real-Time_Workshop') && ...
    license('test','RTW_Embedded_Coder'))
    return
end

初始设置

确保已打开模型的未编辑版本。

model = 'sldv_cruise_control';
close_system(model, 0)
open_system(model)

为基于 SIL 的测试生成配置模型

1.在配置参数窗口中,点击代码生成并将系统目标文件设置为 ert.tlc。或者,输入:

set_param(model,'SystemTargetFile','ert.tlc');

2.点击硬件实现,然后将设备供应商设备类型设置为您的 SIL 系统的供应商和类型。例如,对于 64 位 Linux 计算机,请将设备供应商设置为 Intel,并将设备类型设置为 x-86-64(Windows)。或者,输入:

if ismac
    lProdHWDeviceType = 'Intel->x86-64 (Mac OS X)';
elseif isunix
    lProdHWDeviceType = 'Intel->x86-64 (Linux 64)';
else
     lProdHWDeviceType = 'Intel->x86-64 (Windows64)';
end

set_param(model, 'ProdHWDeviceType', lProdHWDeviceType);

查找用于覆盖率计算的测试用例

使用 Simulink® Design Verifier™ 分析 sldv_cruise_control 模型,以生成可提高代码覆盖率的测试套件。设置 Simulink® Design Verifier™ 选项,以便为顶层模型生成实现 MCDC 覆盖率的测试用例。

opts = sldvoptions;
opts.TestgenTarget = 'GenCodeTopModel';
opts.Mode = 'TestGeneration';
[~, files] = sldvrun(model, opts, true);
### Starting build procedure for: sldv_cruise_control
### Successful completion of build procedure for: sldv_cruise_control
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Searching for referenced models in model 'HARNESS_4_SLDV_SIL_CODEGEN_VALIDATION'.
### Total of 1 models to build.
### Searching for referenced models in model 'sldv_cruise_control'.
### Total of 1 models to build.
### Starting build procedure for: sldv_cruise_control
### Generated code for 'sldv_cruise_control' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for: sldv_cruise_control

Build Summary

0 of 1 models built (1 models already up to date)
Build duration: 0h 0m 2.1644s
### Preparing to start SIL simulation ...
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis

注意:当您运行脚本时,SIL 测试生成会重新生成并重新编译代码。

验证完整覆盖率

sldvruntest 函数使用生成的测试套件来仿真模型。cvhtml 函数生成覆盖率报告,指示 sldvdemo_cruise_control 模型的最终覆盖率。

[~, finalCov] = sldvruntest(model, files.DataFile, [], true);
cvhtml('sil_final_coverage', finalCov);
close_system(model, 0);
### Searching for referenced models in model 'HARNESS_4_SLDV_SIL_CODEGEN_VALIDATION'.
### Total of 1 models to build.
### Searching for referenced models in model 'sldv_cruise_control'.
### Total of 1 models to build.
### Starting build procedure for: sldv_cruise_control
### Generated code for 'sldv_cruise_control' is up to date because no structural, parameter or code replacement library changes were found.
### Successful completion of build procedure for: sldv_cruise_control

Build Summary

0 of 1 models built (1 models already up to date)
Build duration: 0h 0m 1.6951s
### Preparing to start SIL simulation ...
Building with 'gcc'.
MEX completed successfully.
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis
### Starting SIL simulation for component: sldv_cruise_control
### Application stopped
### Stopping SIL simulation for component: sldv_cruise_control
### Completed code coverage analysis

注意:当您运行脚本时,SIL 测试生成会重新生成并重新编译代码。