%% Class-based Unit Testing Of Automotive Algorithms Via CAN
% This example shows you how to validate the output of a cruise control algorithm using the Vehicle Network Toolbox™ and MATLAB™ class-based unit testing framework.
% It uses the MATLAB unit test-class tCruiseControlAlgorithmVerifier.m to provide input commands via Controller Area Network (CAN) to a Simulink™ model of a cruise control algorithm to trigger the functional behavior of the algorithm, and then receives feedback from the model through CAN and validates the expected behavior of the algorithm. It also generates a PDF report of the test results, which can be used for analysis. For more information on how to write the test-class, see the tCruiseControlAlgorithmVerifier.m file. The dialog in that class helps you understand the method of setting up a test-class and what each individual test does.
% This example uses MathWorks™ Virtual CAN Channels to communicate with the algorithm.
%
%% Simulink Model Overview
% The cruise control algorithm has a Virtual CAN Inputs block, which houses the setup of the CAN channel using the CAN Configuration block, and receives the message commanded from the MATLAB test-class using the CAN Receive block. It then uses the CAN Unpack block to separate the individual signals from the received CAN message, which are then converted into their appropriate data types and transmitted to the actual cruise control algorithm.
% The Cruise Control Algorithm block houses the Cruise Control Algorithm State Machine, which is a Stateflow chart. This algorithm works based on the inputs received from the Virtual CAN Inputs block and is set-up to trigger when the input conditions have reached a certain condition. The outputs of the Stateflow chart are the expected vehicle cruising speed, and algorithm engagement state.
% The Virtual CAN Outputs block uses a CAN Pack block to load individual signals into a single CAN message, which are then transmitted onto the CAN bus using the CAN Transmit block. This feedback message is used for verification in the MATLAB test-class.
%
%
%% Creating a Test Suite
% Create a suite of test classes to run. In this example, the tCruiseControlAlgorithmVerifier.m is the only test in the suite. You can add additional tests in the same test suite. The 1xN Test array lists the number of tests, not the number of test-classes.

suite = testsuite({'tCruiseControlAlgorithmVerifier'})

%
%% Creating a Test Runner
% Create a test runner to execute a set of tests in the test suite. This defines the runner with no special plugins.

runner = matlab.unittest.TestRunner.withNoPlugins

%
%% Creating a PDF Report Output
% Set-up the name of the PDF file in which you want your output to be captured.

pdfFile = 'CruiseControlAlgorithmTestReport.pdf'

% Add a PDF creating plugin to your test runner. Firstly, construct the plugin.

plugin =  matlab.unittest.plugins.TestReportPlugin.producingPDF(pdfFile)

% Associate this plugin with the test runner to generate the PDF report in the working directory.

runner.addPlugin(plugin)

%
%% Running Tests
% Run the test suite using the test runner.

result = runner.run(suite)

%
%% Analyzing the PDF Report
% Open the PDF file created with the defined name. The default setting creates it in the current working directory. There are two things to observe in the PDF report:
%% Overall Result and Result Pie Chart
% The overall result states the final outcome based on whether all the tests passed or not. The pie chart shows how many tests passed and failed out of the total number of tests defined.
%
%% Failure Summary Details
% The failure summary shows which test failed and for what reason.
%
% Clicking the details tab on the right hand side of the failure name provides a detailed reason for failure, along with the diagnostic message you configured in the tests.
%
% Copyright 2020 The MathWorks, Inc.