Main Content

matlab.unittest.fixtures.SuppressedWarningsFixture class

Package: matlab.unittest.fixtures

Fixture to suppress display of warnings

Description

The SuppressedWarningsFixture class provides a fixture to suppress the display of warnings. When set up, SuppressedWarningsFixture disables one or more specified warnings. When torn down, the fixture restores the states of warnings to their previous values.

Construction

matlab.unittest.fixtures.SuppressedWarningsFixture(warnIDs) constructs a fixture to suppress the display of one or more warnings.

Input Arguments

expand all

Warning identifiers for the warnings to be suppressed, specified as a character vector or cell array of character vectors.

Properties

Warnings

Warning identifiers describing warnings to suppress specified as a cell array of character vectors in the warnings input argument.

Copy Semantics

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

Examples

collapse all

Suppress the warning that occurs when you try to remove a folder from the search path that is not on the search path.

Remove the folder, folderthatisnotonpath from your path, assuming it does not exist.

 rmpath('folderthatisnotonpath')
Warning: "folderthatisnotonpath" not found in path. 
> In rmpath at 58 

A warning appears because rmpath cannot find the folder.

Suppress the warning during testing by creating the following suppressWarningsTest class definition on your MATLAB® path.

classdef suppressWarningsTest < matlab.unittest.TestCase
    methods(Test)
        function test1(testCase)
            import matlab.unittest.fixtures.SuppressedWarningsFixture
            
            testCase.applyFixture(...
                SuppressedWarningsFixture('MATLAB:rmpath:DirNotFound'));
            
            % would otherwise cause warning
            rmpath('folderthatisnotonpath')  
        end
    end
end

At the command prompt, run the test. For the purposes of this example, call rmpath before and after running the test to show the warning is not suppressed outside execution of the test.

rmpath('folderthatisnotonpath')
run(suppressWarningsTest);
rmpath('folderthatisnotonpath')
Warning: "folderthatisnotonpath" not found in path. 
> In rmpath at 58 
Running suppressWarningsTest
.
Done suppressWarningsTest
__________

Warning: "folderthatisnotonpath" not found in path. 
> In rmpath at 58 

Note that the call to rmpath within suppressWarningsTest does not result in a warning.