Main Content

matlab.unittest.plugins.FailOnWarningsPlugin Class

Namespace: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.QualifyingPlugin, matlab.unittest.plugins.TestRunnerPlugin, matlab.unittest.plugins.Parallelizable

Plugin that fails tests that issue warnings

Description

The matlab.unittest.plugins.FailOnWarningsPlugin class provides a plugin that fails any test that issues a warning. The plugin produces a qualification failure in the test scope that issues the warning. For example, if a shared test fixture issues a warning, the plugin produces a qualification failure on the fixture and fails all tests that share the fixture.

The plugin does not produce a failure if:

  • A test accounts for the warning through a constraint such as IssuesWarnings or IssuesNoWarnings, regardless of whether the constraint is satisfied.

  • The warning is disabled, for example, using the SuppressedWarningsFixture class.

  • The plugin is explicitly instructed to ignore the warning.

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

Creation

Description

plugin = matlab.unittest.plugins.FailOnWarningsPlugin creates a plugin that fails any test that issues a warning.

example

plugin = matlab.unittest.plugins.FailOnWarningsPlugin(Ignoring=identifiers) ignores warnings with the specified identifiers. For example, plugin = matlab.unittest.plugins.FailOnWarningsPlugin(Ignoring="MATLAB:singularMatrix") creates a plugin that does not fail tests because of the specified warning.

Input Arguments

expand all

Warning identifiers to ignore, specified as a string array or cell array of character vectors. The plugin does not fail tests because of the specified warnings.

This argument sets the Ignore property.

Example: "MATLAB:MKDIR:DirectoryExists"

Example: {'MyComponent:FirstID','MyComponent:SecondID'}

Properties

expand all

Warning identifiers to ignore, specified as a string array or cell array of character vectors, and stored as a cell array of character vectors. By default, the value is an empty cell array.

This property is set by the identifiers input argument.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

Fail any test that issues a warning by using the FailOnWarningsPlugin class.

In a file named solver.m in your current folder, create the solver function to solve a system of linear equations. The function includes an intentional bug—the assert call that checks whether a matrix is well conditioned should use rcond instead of det.

function x = solver(A,b)
assert(abs(det(A)) > 1e-12, ... % Intentional bug for illustrative purposes
    "The matrix is singular or nearly singular.")
x = A\b;
end

To test the solver function, create the SolverTest test class in a file named SolverTest.m in your current folder. In the testTwo method, A is a singular matrix. The solver function does not notice the singularity due to the bug in the assert call and causes the test to issue a warning.

classdef SolverTest < matlab.unittest.TestCase
    methods (Test)
        function testOne(testCase)
            A = eye(3);
            b = [3; 4; 1];
            testCase.verifyEqual(solver(A,b),b)
        end

        function testTwo(testCase)
            A = [1e-100 0; 0 1e100];
            b = [5; 5];
            expected = [5e100; 5e-100];
            testCase.verifyEqual(solver(A,b),expected)
        end
    end
end

Import the FailOnWarningsPlugin class.

import matlab.unittest.plugins.FailOnWarningsPlugin

Create a test suite from the test class.

suite = testsuite("SolverTest");

Run the tests using a FailOnWarningsPlugin instance. The plugin fails the second test because it issues a warning. Without the plugin, the test passes.

runner = testrunner("textoutput");
plugin = FailOnWarningsPlugin;
runner.addPlugin(plugin)
results = runner.run(suite);
Running SolverTest
.Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.000000e-200. 
...
 
.
================================================================================
Verification failed in SolverTest/testTwo.
    ---------------------
    Framework Diagnostic:
    ---------------------
    SolverTest/testTwo issued warnings:    
        
        ---------------------------
        MATLAB:nearlySingularMatrix
        ---------------------------
        Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.000000e-200.
            In C:\work\solver.m (solver) at 4
            In C:\work\SolverTest.m (SolverTest.testTwo) at 13
    ------------------
    Stack Information:
    ------------------
    In C:\Program Files\MATLAB\R2024b\toolbox\matlab\testframework\unittest\core\+matlab\+unittest\+plugins\FailOnWarningsPlugin.m (FailOnWarningsPlugin.teardownTestMethod) at 178
================================================================================

Done SolverTest
__________

Failure Summary:

     Name                Failed  Incomplete  Reason(s)
    =================================================================
     SolverTest/testTwo    X                 Failed by verification.

Tips

Version History

Introduced in R2015b