matlab.unittest.diagnostics.FunctionHandleDiagnostic class

Package: matlab.unittest.diagnostics
Superclasses: matlab.unittest.diagnostics.Diagnostic

Diagnostic using a function's displayed output

Description

The FunctionHandleDiagnostic class provides a diagnostic result using a function's displayed output. This output is the same as the text displayed at the command prompt when MATLAB® executes the function handle. When the diagnostic information is accessible through information displayed as output of the function handle, the FunctionHandleDiagnostic is a means to provide quick diagnostic information.

When using matlab.unittest qualifications, a function handle can be supplied directly as a test diagnostic. In this case, the testing framework automatically creates a FunctionHandleDiagnostic object.

Construction

FunctionHandleDiagnostic(fcnHandle) creates a new FunctionHandleDiagnostic instance.

Input Arguments

fcnHandle

The function handle that the Diagnostic uses to generate diagnostic information.

The resulting diagnostic information is equivalent to output displayed at the MATLAB command prompt. The result is packaged for consumption by the testing framework, which may or may not display the information at the command prompt.

Properties

Fcn

The function handle that the Diagnostic uses to generate diagnostic information, specified in the fcnHandle input argument. This property is read-only.

Inherited 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

Inherited 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

Create FunctionHandleDiagnostic Object

Create a diagnostic result that displays the output of the dir function when a test fails.

Create a folder in your current working folder.

mkdir('subfolderInCurrentFolder')

Create a test case for interactive testing.

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsEqualTo
import matlab.unittest.diagnostics.FunctionHandleDiagnostic

testCase = TestCase.forInteractiveUse;

Use a FunctionHandleDiagnostic to display diagnostic information upon test failure.

testCase.verifyThat(1, IsEqualTo(2), FunctionHandleDiagnostic(@dir))
Interactive verification failed.

----------------
Test Diagnostic:
----------------

.                         ..                        subfolderInCurrentFolder  



---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> NumericComparator failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                1        1         2           -1       -0.5         

Actual Value:
         1
Expected Value:
         2

Upon test failure, the diagnostic displays the contents of the current working folder. In this example output, the folder only contains the subfolder subfolderInCurrentFolder.

Alternatively, the test framework can create a FunctionHandleDiagnostic object for you from a function handle input to the verifyThat qualification.

testCase.verifyThat(1, IsEqualTo(2), @dir)
Interactive verification failed.

----------------
Test Diagnostic:
----------------

.                         ..                        subfolderInCurrentFolder  



---------------------
Framework Diagnostic:
---------------------
IsEqualTo failed.
--> NumericComparator failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                1        1         2           -1       -0.5         

Actual Value:
         1
Expected Value:
         2

The testing framework only creates the FunctionHandleDiagnostic object as needed, typically only in the event of a test failure.

Was this topic helpful?