Main Content

matlab.unittest.plugins.FailOnWarningsPlugin Class

Namespace: matlab.unittest.plugins

Plugin to fail tests that issue warnings

Description

The FailOnWarningsPlugin creates a plugin that, when added to the TestRunner, 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 FailOnWarningsPlugin 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 or not.

  • A warning is disabled. For example, if you disable a warning using the SuppressedWarningsFixture.

Construction

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

matlab.unittest.plugins.FailOnWarningsPlugin('Ignoring',warnIDs) creates a plugin that does not fail for the specified warning identifiers, warnIDs.

Input Arguments

expand all

Identifiers for warnings to ignore, specified as a cell array of character vectors. The plugin does not fail a test for the warnings with identifiers included in warnIDs.

Example: FailOnWarningsPlugin('Ignoring',{'MATLAB:singularMatrix'})

Properties

expand all

The Ignore property is empty by default. To specify the property as a cell array of character vectors, use the 'Ignoring' syntax when you construct the plugin instance.

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Create the solve function to solve a set of linear equations. There is an intentional error in the solve function — the assert call that checks whether the matrix is singular should use rcond instead of det.

function x = solve(A,b)

assert(abs(det(A)) > 1e-12,... % intentional bug for illustrative purposes
    'The matrix is singular or nearly singular'); 

x = A\b;

Create the following test class. In testTwo, the A matrix is singular, but since there is a bug in the solve function, the assert call does not catch it.

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

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

At the command prompt, create a test suite, and test runner.

import matlab.unittest.TestRunner;
import matlab.unittest.TestSuite;
import matlab.unittest.plugins.FailOnWarningsPlugin;

suite = TestSuite.fromClass(?TestSolve);
runner = TestRunner.withTextOutput;

Add the FailOnWarningsPlugin plugin, and run the tests. testTwo fails because the solve function issues a warning. Without FailOnWarningsPlugin, the solve function issues the warning, but both tests pass.

runner.addPlugin(FailOnWarningsPlugin);
result = runner.run(suite);
Running TestSolve
.Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.000000e-200.
.
================================================================================
Verification failed in TestSolve/testTwo.

    ---------------------
    Framework Diagnostic:
    ---------------------
    TestSolve/testTwo issued warnings:    
        
        ---------------------------
        MATLAB:nearlySingularMatrix
        ---------------------------
        Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.000000e-200.
            In C:\work\solve.m (solve) at 6
            In C:\work\TestSolve.m (TestSolve.testTwo) at 14

    ------------------
    Stack Information:
    ------------------
    In C:\Program Files\MATLAB\R2015b\toolbox\matlab\testframework\+matlab\+unittest\+plugins\FailOnWarningsPlugin.m (FailOnWarningsPlugin.teardownTestMethod) at 164
================================================================================

Done TestSolve
__________

Failure Summary:

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

Tips

  • Set the warning stack trace display using the warning function. For example, warning('off','backtrace').

  • Disable warnings using the SuppressedWarningsFixture class.

Version History

Introduced in R2015b