matlab.unittest.plugins.FailureDiagnosticsPlugin class

Package: matlab.unittest.plugins

Plugin to show diagnostics on failure

Description

The FailureDiagnosticsPlugin creates a plugin to show diagnostics upon encountering a test failure. Add it to the TestRunner to output test failure diagnostics to the Command Window. This plugin is used by default when you construct a test runner using TestRunner.withTextOutput.

Construction

matlab.unittest.plugins.FailureDiagnosticsPlugin creates a plugin to show diagnostics upon encountering a test failure.

matlab.unittest.plugins.DFailureDiagnosticsPlugin(stream) redirects all the text output to the output stream, stream. If you do not specify the output stream, the plugin uses the ToStandardOutput stream.

Input Arguments

stream

Location where the plugin directs text output, specified as an OutputStream.

Default: ToStandardOutput

Copy Semantics

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

Examples

expand all

Add Plugin to TestRunner

In your working folder, create a file, ExampleTest.m, containing the following test class.

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testPathAdd(testCase)
            % test code
        end
        function testOne(testCase)  % Test fails
            testCase.verifyEqual(5, 4, 'Testing 5==4')
        end
         function testTwo(testCase)  % Test passes
             testCase.verifyEqual(5, 5, 'Testing 5==5')
        end
    end
end

The verifyEqual qualification in testOne causes a test failure. The qualifications in testOne and testTwo include an instance of a matlab.unittest.diagnostics.StringDiagnostic.

At the command prompt, create a test suite from the ExampleTest class.

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.FailureDiagnosticsPlugin

suite = TestSuite.fromClass(?ExampleTest);

Create a test runner with no plugins. This code creates a silent runner and provides you with complete control over the installed plugins.

runner = TestRunner.withNoPlugins;

Run the tests.

result1 = runner.run(suite);

No output is displayed, but result1 contains information about the failed test.

Add FailureDiagnosticsPlugin to the runner and run the tests.

runner.addPlugin(FailureDiagnosticsPlugin)
result2 = runner.run(suite);
================================================================================
Verification failed in ExampleTest/testOne.

    ----------------
    Test Diagnostic:
    ----------------
    Testing 5==4

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                1        5         4           1        0.25         
    
    Actual Value:
             5
    Expected Value:
             4

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 7
================================================================================
Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
    ==================================================================
     ExampleTest/testOne    X                 Failed by verification.

The framework displays the DiagnosticResult of the StringDiagnostic for failed tests only. It also displays additional framework diagnostics. The TestResult object, result2, is the same as result1.

Was this topic helpful?