Main Content

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

多组分批次等效性测试

此示例显示如何使用 sltest.testmanager.createTestForComponent 以批处理模式为多个组件创建等效性测试用例和测试框架。示例中还包括覆盖率收集和报告生成。

本例中使用的 sltestCruiseControl 模型具有原子 Subsystem 模块、虚拟子系统模块和模型参考模块。

请注意,此示例仅适用于 Windows 机器。

打开模型

topModel = "sltestCruiseControl";
load_system(topModel);

生成代码

本例中的等效性测试比较了普通模式代码和生成的代码。Embedded Coder 是生成模型代码所必需的。该模型已配置适合 Windows 64 位系统的 C 代码生成和编码器映射设置。如果需要,您可以在生成代码之前更改这些设置。

slbuild(topModel);
### Starting serial model reference code generation build.
### Checking status of model reference code generation target for model 'sltestCruiseControlMode' used in 'sltestCruiseControl'.
### Model reference code generation target (sltestCruiseControlMode.c) for model sltestCruiseControlMode is out of date because sltestCruiseControlMode.c does not exist.
### Starting build procedure for: sltestCruiseControlMode
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\slprj\ert\sltestCruiseControlMode
### Invoking Target Language Compiler on sltestCruiseControlMode.rtw
### Using System Target File: M:\29\dschwart.Bdoc23b.j2311661\matlab\rtw\c\ert\ert.tlc
### Loading TLC function libraries
.......
### Initial pass through model to cache user defined code
...
### Caching model source code
..............................................................................
### Writing header file sltestCruiseControlMode_types.h
### Writing header file sltestCruiseControlMode.h
### Writing header file rtwtypes.h
### Writing header file sltestCruiseControlMode_private.h
.
### Writing source file sltestCruiseControlMode.c
### TLC code generation complete (took 7.231s).
..### Creating HTML report file index.html
### Saving binary information cache.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\slprj\ert\sltestCruiseControlMode\sltestCruiseControlMode.mk' ...
### Successful completion of code generation for: sltestCruiseControlMode
### Checking status of model reference code generation target for model 'sltestDriverSwRequest' used in 'sltestCruiseControl'.
### Model reference code generation target (sltestDriverSwRequest.c) for model sltestDriverSwRequest is out of date because sltestDriverSwRequest.c does not exist.
### Starting build procedure for: sltestDriverSwRequest
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\slprj\ert\sltestDriverSwRequest
### Invoking Target Language Compiler on sltestDriverSwRequest.rtw
### Using System Target File: M:\29\dschwart.Bdoc23b.j2311661\matlab\rtw\c\ert\ert.tlc
### Loading TLC function libraries
.......
### Initial pass through model to cache user defined code
..
### Caching model source code
.................................................
### Writing header file sltestDriverSwRequest_types.h
### Writing source file sltestDriverSwRequest.c
.
### Writing header file sltestDriverSwRequest_private.h
### Writing header file sltestDriverSwRequest.h
### TLC code generation complete (took 6.534s).
### Creating HTML report file index.html
### Saving binary information cache.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\slprj\ert\sltestDriverSwRequest\sltestDriverSwRequest.mk' ...
### Successful completion of code generation for: sltestDriverSwRequest
### Simulink cache artifacts for 'sltestCruiseControlMode' were created in 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\sltestCruiseControlMode.slxc'.
### Simulink cache artifacts for 'sltestDriverSwRequest' were created in 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\sltestDriverSwRequest.slxc'.
### Starting build procedure for: sltestCruiseControl
### Generating code and artifacts to 'Model specific' folder structure
### Generating code into build folder: C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\sltestCruiseControl_ert_rtw
### Invoking Target Language Compiler on sltestCruiseControl.rtw
### Using System Target File: M:\29\dschwart.Bdoc23b.j2311661\matlab\rtw\c\ert\ert.tlc
### Loading TLC function libraries
........
### Generating TLC interface API for custom data
.
### Initial pass through model to cache user defined code
..
### Caching model source code
...............................................................................
.................
### Writing header file sltestCruiseControl_types.h
### Writing header file sltestCruiseControl.h
### Writing source file sltestCruiseControl.c
.
### Writing header file sltestCruiseControl_private.h
### Writing source file ert_main.c
### TLC code generation complete (took 9.486s).
### Saving binary information cache.
### Using toolchain: MinGW64 | gmake (64-bit Windows)
### Creating 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\sltestCruiseControl_ert_rtw\sltestCruiseControl.mk' ...
### Successful completion of code generation for: sltestCruiseControl
### Simulink cache artifacts for 'sltestCruiseControl' were created in 'C:\Users\dschwart\OneDrive - MathWorks\Documents\MATLAB\ExampleManager\dschwart.Bdoc23b.j2311661\simulinktest-ex89766869\sltestCruiseControl.slxc'.

Build Summary

Code generation targets built:

Model                    Action           Rebuild Reason                             
=====================================================================================
sltestCruiseControlMode  Code generated.  sltestCruiseControlMode.c does not exist.  
sltestDriverSwRequest    Code generated.  sltestDriverSwRequest.c does not exist.    

Top model targets built:

Model                Action           Rebuild Reason                                    
========================================================================================
sltestCruiseControl  Code generated.  Code generation information file does not exist.  

3 of 3 models built (0 models already up to date)
Build duration: 0h 2m 30.175s

指定要测试的组件

该示例测试了生成的代码中具有不可重用函数包装的所有原子子系统。

componentsToTest = find_system(topModel,...
    "BlockType","SubSystem",...
    "TreatAsAtomicUnit","on",...
    "RTWSystemCode","Nonreusable function");

为了提高测试工件(例如测试用例和测试框架名称)的可追溯性,请自定义所创建的框架的默认名称。名称将使用组件名称而不是所属模型名称。

sltest.harness.setHarnessCreateDefaults("Name","$Component$_Harness");

以批处理模式创建测试用例和测试框架

使用 sltest.testmanager.createTestForComponent API 可以同时创建多个测试用例和测试框架。使用 createTestForComponent,您可以指定创建一个测试文件和测试文件名。您还可以指定顶层模型、要测试的组件以及测试类型和仿真设置。对于测试输入,您指定使用 Simulink Design Verifier 自动生成 Microsoft Excel 格式的输入。如果您有其他现有输入,则可以将它们添加到测试用例中并测试这些输入和生成的输入。

[tc, status] = ...
    sltest.testmanager.createTestForComponent(...
        "CreateTestFile",true,...
        "TestFile","myB2BTestsDemoEx.mldatx",...
        "TopModel",topModel,...
        "Component",componentsToTest,...
        "TestType","equivalence",...
        "Simulation1Mode","Normal",...
        "Simulation2Mode","Software-in-the-Loop (SIL)",...
        "SLDVTestGeneration","on",...
        "CreateExcelFile",true);

测试执行

createTestForComponent API 在当前工作目录中生成了测试文件、测试框架和测试输入信号。现在,保存附加到模型的框架,并保存测试文件。

tf = sltest.testmanager.TestFile("myB2BTestsDemoEx.mldatx");
tf.saveToFile;

启用测试文件的覆盖率收集并运行测试。

cov = getCoverageSettings(tf);
cov.RecordCoverage = true;
cov.MetricSettings = "dcmr";
tf.saveToFile;

resultSet = tf.run;
sltest.testmanager.exportResults(resultSet,"myB2BResults.mldatx");
sltestmgr;

生成包含覆盖率和等效性测试结果的报告。

sltest.testmanager.report(...
    resultSet,"myB2BResultsReport.pdf",...
    "IncludeCoverageResult",true,...
    "IncludeSimulationSignalPlots",true,...
    "IncludeComparisonSignalPlots",true,...
    "IncludeTestResults",0,...
    "IncludeSimulationMetadata",true);

Results summary showing aggregated coverage of 100%

清理

bdclose({topModel,'sltestDriverSwRequest','sltestCruiseControlMode'})
sltest.testmanager.clear
sltest.testmanager.clearResults
sltest.testmanager.close

另请参阅