%% Test a Model for Continuous Integration Systems
% This example shows how to test a model, publish Test Manager results, and output results in TAP format with a single execution.
% You use MATLAB® Unit Test to create a test suite and a test runner, and customize the runner with these plugins:
% TestReportPlugin produces a MATLAB Test Report.
% TestManagerResultsPlugin adds Test Manager results to the MATLAB Test Report.
% TAPPlugin outputs results to a TAP file.
% The test case creates a square wave input to a controller subsystem and sweeps through 25 iterations of parameters a and b. The test compares the alpha output to a baseline with a tolerance of 0.0046. The test fails on those iterations in which the output exceeds this tolerance.
% Before running this example, ensure that the working folder is writable.
%
% 1. Open the Simulink® Test™ test file.

testfile = fullfile('f14ParameterSweepTest.mldatx');
sltest.testmanager.view;
sltest.testmanager.load(testfile);

%
% 2. In the Test Manager, configure the test file for reporting.
% Under Test File Options, select Generate report after execution. The section expands, displaying several report options. For more information, see Save Reporting Options with a Test File.
%
% 3. Create a test suite from the Simulink® Test™ test file.

import matlab.unittest.TestSuite

suite = testsuite('f14ParameterSweepTest.mldatx');

%
% 4. Create a test runner.

import matlab.unittest.TestRunner

f14runner = TestRunner.withNoPlugins;

%
% 5. Add the TestReportPlugin to the test runner.
% The plugin produces a MATLAB Test Report F14Report.pdf.

import matlab.unittest.plugins.TestReportPlugin

pdfFile = 'F14Report.pdf';
trp = TestReportPlugin.producingPDF(pdfFile);
addPlugin(f14runner,trp)

%
% 6. Add the TestManagerResultsPlugin to the test runner.
% The plugin adds Test Manager results to the MATLAB Test Report.

import sltest.plugins.TestManagerResultsPlugin

tmr = TestManagerResultsPlugin; 
addPlugin(f14runner,tmr)

%
% 7. Add the TAPPlugin to the test runner.
% The plugin outputs to the F14Output.tap file.

import matlab.unittest.plugins.TAPPlugin
import matlab.unittest.plugins.ToFile

tapFile = 'F14Output.tap';
tap = TAPPlugin.producingVersion13(ToFile(tapFile));
addPlugin(f14runner,tap)

%
% 8. Run the test.
% Several iterations fail, in which the signal-baseline difference exceeds the tolerance criteria.

result = run(f14runner,suite);

% A single execution of the test runner produces two reports:
% A MATLAB Test Report that contains Test Manager results.
% A TAP format file that you can use with CI systems.
%

sltest.testmanager.clearResults
sltest.testmanager.clear
sltest.testmanager.close

%
% Copyright 2020 The MathWorks, Inc.