主要内容

在 Stateflow 图中为自定义代码生成测试

此示例说明如何在 Stateflow® 图中在包含自定义代码的模型上使用测试生成。

在 Stateflow 图中打开包含自定义代码的模型

open_system('sldvSFCustomCodeExample');

生成测试以确保模型的覆盖率

使用 sldvrun 函数运行 Simulink® Design Verifier™ 分析。

opts = sldvoptions;
opts.Mode = 'TestGeneration';
opts.ModelCoverageObjectives = 'ConditionDecision';
opts.SaveHarnessModel = 'off';
opts.SaveReport = 'off';

[status, fileNames] = sldvrun('sldvSFCustomCodeExample', opts);
12-Aug-2025 18:18:06
Checking compatibility for test generation: model 'sldvSFCustomCodeExample'
Compiling model...done
Building model representation...done

12-Aug-2025 18:18:19

'sldvSFCustomCodeExample' is compatible for test generation with Simulink Design Verifier.

Generating tests using model representation from 12-Aug-2025 18:18:19...

..........
12-Aug-2025 18:18:27

Completed normally.

Generating output files:

12-Aug-2025 18:18:29
Results generation completed.

    Data file:
    /tmp/Bdoc25b_2988451_683368/tpfb6fc956/sldv-ex18712703/sldv_output/sldvSFCustomCodeExample/sldvSFCustomCodeExample_sldvdata.mat

验证覆盖率

使用 sldvruntest 函数验证测试套件是否实现了完整模型覆盖率。

[~, finalCov] = sldvruntest('sldvSFCustomCodeExample', fileNames.DataFile, [], true);
cvhtml('Final Coverage', finalCov);

清理

要完成示例,请关闭所有模型。

close_system('sldvSFCustomCodeExample', 0);