matlab.unittest.diagnostics.Diagnostic class

Package: matlab.unittest.diagnostics

Fundamental interface class for matlab.unittest diagnostics

Description

The Diagnostic interface class is the means by which the matlab.unittest framework and its clients package diagnostic information. All diagnostics are derived from Diagnostic, whether they are user-supplied test diagnostics for an individual comparison or diagnostics associated with the Constraint used in the comparison.

Classes which derive from Diagnostic encode the diagnostic actions to be performed. They produce a diagnostic result that is displayed appropriately by the test running framework. In exchange for meeting this requirement, any Diagnostic implementation can be used directly with matlab.unittest qualifications. These qualifications execute the diagnostic action and store the result to be utilized by the test running framework.

As a convenience, the framework creates appropriate diagnostic instances for strings and function handles when they are user supplied test diagnostics. To retain good performance, these values are only converted into Diagnostic instances when a qualification failure occurs or when the test running framework is explicitly observing passing qualifications. The default test runner does not explicitly observe passing qualifications.

Properties

DiagnosticResult

The DiagnosticResult property provides the means by which the actual diagnostic information is communicated to consumers of diagnostics, such as testing frameworks. The property is a string that is defined during evaluation of the diagnose method.

Methods

diagnoseExecute diagnostic action
joinJoin multiple diagnostics into a single array

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects in the MATLAB® documentation.

Examples

expand all

Use Diagnostic Result

import matlab.unittest.constraints.IsEqualTo

% Create a TestCase for interactive use
testCase = matlab.unittest.TestCase;

% Create StringDiagnostic upon failure
testCase.verifyThat(1, IsEqualTo(2), 'User supplied Diagnostic')

% Create FunctionHandleDiagnostic upon failure
testCase.verifyThat(1, IsEqualTo(2), @() system('ps'))

% Usage of user defined Diagnostic upon failure (see definition below)
testCase.verifyThat(1, IsEqualTo(2), ProcessStatusDiagnostic...
    ('Could not close my third party application!'))

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Diagnostic definition
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
classdef ProcessStatusDiagnostic < matlab.unittest.diagnostics.Diagnostic
    % ProcessStatusDiagnostic - an example diagnostic
    %
    %   Simple example to demonstrate how to create a custom
    %   diagnostic.
    
    properties
        
        % HeaderText - user-supplied header to display
        HeaderText = '(No header supplied)';
    end
    
    methods
        function diag = ProcessStatusDiagnostic(header)
            % Constructor - construct a ProcessStatusDiagnostic
            %
            %   The ProcessStatusDiagnostic constructor takes an
            %   optional header to be displayed along with process
            %   information.
            if (nargin >0)
                diag.HeaderText = header;
            end
        end
        
        function diagnose(diag)
            
            [status, processInfo] = system('ps');
            if (status ~= 0)
                processInfo = sprintf(...
                    ['!!! Could not obtain status diagnostic information!!!'...
                    ' [exit status code: %d]\n%s'], status, processInfo);
            end
            diag.DiagnosticResult = sprintf('%s\n%s', diag.HeaderText,...
                processInfo);
        end
    end
    
end % classdef
Was this topic helpful?