Main Content

本页采用了机器翻译。点击此处可查看最新英文版本。

代码覆盖率测试生成

此示例展示如何使用 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'.
### Found 0 model references to update.
### 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.8706s
### 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'.
### Found 0 model references to update.
### 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.9835s
### 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 测试生成会重新生成并重新编译代码。