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);
20-Jul-2024 19:48:27
Checking compatibility for test generation: model 'sldvexSpreadsheetTopoff'
Compiling model...done
Building model representation...done

20-Jul-2024 19:48:37

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

Generating tests using model representation from 20-Jul-2024 19:48:37...


Generating output files:

20-Jul-2024 19:48:56
Results generation completed.

    Data file:
    /tmp/Bdoc24b_2679053_1509958/tp72f6dff9/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);
20-Jul-2024 19:49:13
Checking compatibility for test generation: model 'sldvexSpreadsheetTopoff'
Compiling model...done
Building model representation...done

20-Jul-2024 19:49:17

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

20-Jul-2024 19:49:17
Loading initial test data...done

Generating tests using model representation from 20-Jul-2024 19:49:17...


Generating output files:

20-Jul-2024 19:49:25
Results generation completed.

    Data file:
    /tmp/Bdoc24b_2679053_1509958/tp72f6dff9/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.