Code Coverage Test Generation
This example shows how to use Simulink® Design Verifier™ to generate test cases to obtain complete code coverage.
You first collect code coverage for an example model configured for software-in-the-loop (SIL) simulation mode. Then you use Simulink® Design Verifier™ to create a test suite that generates tests cases. Finally, you execute the generated test cases in SIL simulation mode to verify the complete coverage.
Check Product Availability
Make sure that you have Simulink® Coder™ and Embedded Coder™ software installed on your machine.
if ~(license('test', 'Real-Time_Workshop') && ... license('test','RTW_Embedded_Coder')) return end
Initial Setup
Make sure that an unedited version of the model is open.
model = 'sldv_cruise_control';
close_system(model, 0)
open_system(model)
Configure the Model for SIL based test generation
1. In the Configuration Parameters window, click Code Generation and set System Target File to ert.tlc
. Alternatively, enter:
set_param(model,'SystemTargetFile','ert.tlc');
2. Click Hardware Implementation, then set Device vendor and Device type to the vendor and type of your SIL system. For example, for a 64-bit Linux machine, set Device vendor to Intel
and Device type to x-86-64(Windows)
. Alternatively, enter:
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);
Find Test Cases for Coverage Computation
Analyze the sldv_cruise_control
model by using Simulink® Design Verifier™ to generate a test suite that achieves increased code coverage. Set the Simulink® Design Verifier™ options to generate test cases to achieve MCDC coverage for the top model.
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 1 model reference targets 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.0617s ### 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
Note: When you run the script, the SIL test generation regenerates and recompiles the code.
Verify Complete Coverage
The sldvruntest
function simulates the model by using the generated test suite. The cvhtml
function produces a coverage report that indicates the final coverage of the sldvdemo_cruise_control
model.
[~, 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 1 model reference targets 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.8932s ### 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
Note: When you run the script, the SIL test generation regenerates and recompiles the code.