Fixture to suppress display of warnings
SuppressedWarningsFixture class provides
a fixture to suppress the display of warnings. When set up,
one or more specified warnings. When torn down, the fixture restores
the states of warnings to their previous values.
a fixture to suppress the display of one or more warnings.
warnIDs— Identifier for warnings disabled when the fixture is set up
Warning identifiers for the warnings to be suppressed, specified as a character vector or cell array of character vectors.
Warning identifiers describing warnings to suppress specified
as a cell array of character vectors in the
Handle. To learn how handle classes affect copy operations, see Copying Objects.
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,
your path, assuming it does not exist.
Warning: "folderthatisnotonpath" not found in path. > In rmpath at 58
A warning appears because
rmpath cannot find
Suppress the warning during testing by creating the following
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
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
not result in a warning.