Main Content

Add Test Cases Using Excel File

This example shows how to create test cases incrementally by using Simulink® Design Verifier™ supported Excel® file.

Simulink® Design Verifier™ generates test cases to satisfy testing criteria, such as model objectives. Owing to support limitations and model complexity, occasionally it can generate test cases that do not cover all model objectives. Use this example to understand how to:

  • Create test cases in Excel file format.

  • Write a new test case manually in an Excel file.

  • Create additional test cases by using test extension with Excel file.

Generate Test Cases by Using Design Verifier

Open the model and create test cases.

model = 'sldvexSpreadsheetTopoff';
open_system(model);
[~, files] = sldvrun(model);
13-Feb-2024 01:12:21
Checking compatibility for test generation: model 'sldvexSpreadsheetTopoff'
Compiling model...done
Building model representation...done

13-Feb-2024 01:12:44

'sldvexSpreadsheetTopoff' is partially compatible for test generation with Simulink Design Verifier.

The model can be analyzed by Simulink Design Verifier. 
It contains unsupported elements that will be stubbed out during analysis. The results of the analysis might be incomplete. 
See the Diagnostic Viewer for more details on the unsupported elements.

Generating tests using model representation from 13-Feb-2024 01:12:44...


Generating output files:

13-Feb-2024 01:13:12
Results generation completed.

    Data file:
    /tmp/Bdoc24a_2528353_1829948/tp6e5eb6c3/sldv-ex79932255/sldv_output/sldvexSpreadsheetTopoff/sldvexSpreadsheetTopoff_sldvdata.mat

Save Design Verifier Test Cases to Excel File

Simulink Design Verifier creates test cases in the MAT-file format by default. Save the test cases generated in the previous section in an Excel file by using any of these methods:

  • Click Save to spreadsheet button in Results.

  • Click Save to spreadsheet link in the results window, or results inspector.

  • Use sldvgenspreadsheet function.

For this example, use the sldvgenspreadsheet function to save the test cases.

excelFilePath = sldvgenspreadsheet(model, files.DataFile);

Note: Importing or exporting to an Excel file is not supported for an array of bus signals. For more information, see Microsoft Excel Import, Export, and Logging Format.

Identify Missing Coverage Objectives

Simulate the model by using all test cases from the Excel file and create a coverage report. sldvruntest supports test cases from a spreadsheet as simulation input.

runOpts = sldvruntestopts;
runOpts.coverageEnabled = true; % Enable coverage
[~, initialCov] = sldvruntest(model, excelFilePath, runOpts); % Use test cases from Excel file for simulation
cvhtml('Initial coverage', initialCov);

Observe that the value of Switch block logical trigger input is never false in the coverage report.

switchblock.png

Write Test Case to Satisfy Coverage Objective

Determine the cause of missing the coverage objective. In this example, the model contains unsupported block Sqrt, which limits Simulink Design Verifier analysis.

To make the value of trigger input of Switch block false, observe that the value of inport In3 should be greater than 100. Add a new sheet in the Excel file with the test case.

Verify whether the new test case satisfies the required coverage objective.

excelFilePath = 'WithNewTestCase.xlsx';
runOpts = sldvruntestopts;
runOpts.testIdx = 2; % Simulate only the newly added test case
runOpts.coverageEnabled = true;
[~, newTestCov] = sldvruntest(model, excelFilePath, runOpts);
cvhtml('New test coverage', newTestCov);

Run Test Extension by Using Excel file

Simulink Design Verifier test extension workflow generates new test cases by extending the existing test cases. This helps to satisfy additional coverage objectives by extending your new test case.

opts = sldvoptions(model);
opts.ExistingTestFile = excelFilePath; % Use Excel file with new test cases as input for test extension
opts.ExtendExistingTests = 'on'; % Enable test extension
[~, files] = sldvrun(model, opts);
13-Feb-2024 01:13:36
Checking compatibility for test generation: model 'sldvexSpreadsheetTopoff'
Compiling model...done
Building model representation...done

13-Feb-2024 01:13:40

'sldvexSpreadsheetTopoff' is partially compatible for test generation with Simulink Design Verifier.

The model can be analyzed by Simulink Design Verifier. 
It contains unsupported elements that will be stubbed out during analysis. The results of the analysis might be incomplete. 
See the Diagnostic Viewer for more details on the unsupported elements.

13-Feb-2024 01:13:40
Loading initial test data...done

Generating tests using model representation from 13-Feb-2024 01:13:40...


Generating output files:

13-Feb-2024 01:13:49
Results generation completed.

    Data file:
    /tmp/Bdoc24a_2528353_1829948/tp6e5eb6c3/sldv-ex79932255/sldv_output/sldvexSpreadsheetTopoff/sldvexSpreadsheetTopoff_sldvdata1.mat

Verify Complete Coverage

Simulate the model using the new test cases and verify that you now have complete coverage.

runOpts = sldvruntestopts;
runOpts.coverageEnabled = true; % Enable coverage
[~, finalCov] = sldvruntest(model, files.DataFile, runOpts);
cvhtml('Final coverage', finalCov);
close_system(model, 0);

If the new test cases still yield partial coverage, you can write a new test case in an Excel file and then run test extension workflow till complete coverage is achieved.