%% Model Coverage Results for Continuous Integration
% This example shows how to generate model coverage results for use with continuous integration. Coverage is reported in the Cobertura format. You run a Simulink® Test™ test file using MATLAB® Unit Test.
%
% 1. Import classes and create a test suite from the test file AutopilotTestFile.mldatx.

import matlab.unittest.TestRunner

aptest = sltest.testmanager.TestFile('AutopilotTestFile.mldatx');
apsuite = testsuite(aptest.FilePath);

%
% 2. Create a test runner.

trun = TestRunner.withNoPlugins;

%
% 3. Set the coverage metrics to collect. This example uses decision coverage. In the Cobertura output, decision coverage is listed as condition elements.

import sltest.plugins.coverage.CoverageMetrics

cmet = CoverageMetrics('Decision',true);

%
% 4. Set the coverage report properties. This example produces a file R13Coverage.xml in the current working folder. Ensure your working folder has write permissions.

import sltest.plugins.coverage.ModelCoverageReport
import matlab.unittest.plugins.codecoverage.CoberturaFormat

rptfile = 'R13Coverage.xml';
rpt = CoberturaFormat(rptfile)

%
% 5. Create a model coverage plugin. The plugin collects the coverage metrics and produces the Cobertura format report.

import sltest.plugins.ModelCoveragePlugin

mcp = ModelCoveragePlugin('Collecting',cmet,'Producing',rpt)

%
% 6. Add the coverage plugin to the test runner.

addPlugin(trun,mcp)

% Turn off command line warnings:
warning off Stateflow:cdr:VerifyDangerousComparison
warning off Stateflow:Runtime:TestVerificationFailed

%
% 7. Run the test.

APResult = run(trun,apsuite)

%
% 8. Reenable warnings.

warning on Stateflow:cdr:VerifyDangerousComparison
warning on Stateflow:Runtime:TestVerificationFailed

%
% Copyright 2018 The MathWorks, Inc.