Main Content

matlab.unittest.plugins.FailureDiagnosticsPlugin class

Package: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.TestRunnerPlugin, matlab.unittest.plugins.Parallelizable

(Not recommended) Plugin to display diagnostics on failure

matlab.unittest.plugins.FailureDiagnosticsPlugin is not recommended. Use matlab.unittest.plugins.DiagnosticsOutputPlugin instead.

Description

The matlab.unittest.plugins.FailureDiagnosticsPlugin class provides a plugin to display diagnostics upon a test failure.

The matlab.unittest.plugins.FailureDiagnosticsPlugin class is a handle class.

Creation

Description

example

plugin = matlab.unittest.plugins.FailureDiagnosticsPlugin creates a plugin to display diagnostics upon a test failure. The plugin directs its text output to the screen.

plugin = matlab.unittest.plugins.FailureDiagnosticsPlugin(stream) creates a plugin that writes its data to the specified output stream.

Input Arguments

expand all

Location where the plugin directs text output, specified as a matlab.automation.streams.OutputStream object. By default, the plugin directs its output to the screen.

Examples

collapse all

Display diagnostic information upon a test failure by using the FailureDiagnosticsPlugin class.

In a file named ExampleTest.m in your current folder, create the ExampleTest class. The class contains a test that fails and another test that passes. In each test, the verifyEqual method is supplied with an optional string diagnostic.

classdef ExampleTest < matlab.unittest.TestCase
    methods (Test)
        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

Import the FailureDiagnosticsPlugin class.

import matlab.unittest.plugins.FailureDiagnosticsPlugin

Create a test suite from the ExampleTest class.

suite = testsuite("ExampleTest");

Create a minimal test runner with no plugins installed, and use it to run the tests. Because you use a silent runner, the test run does not result in any text output.

runner = testrunner("minimal");
results1 = runner.run(suite);

Now, add a FailureDiagnosticsPlugin instance to the test runner and run the tests. The plugin enables the framework to display diagnostics upon the test failure. When testOne fails, the text output includes the user-supplied diagnostic for the failing test as well as additional framework diagnostics.

runner.addPlugin(FailureDiagnosticsPlugin)
result2 = runner.run(suite);
================================================================================
Verification failed in ExampleTest/testOne.
    ----------------
    Test Diagnostic:
    ----------------
    Testing 5==4
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The numeric values are not equal using "isequaln".
    --> Failure table:
            Actual    Expected    Error    RelativeError
            ______    ________    _____    _____________
                                                        
              5          4          1          0.25     
    
    Actual Value:
         5
    Expected Value:
         4
    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
================================================================================
Failure Summary:

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

Version History

Introduced in R2013a

collapse all

R2018b: Not recommended

The matlab.unittest.plugins.FailureDiagnosticsPlugin class is not recommended. Use the matlab.unittest.plugins.DiagnosticsOutputPlugin class instead.