Main Content

matlab.unittest.fixtures.SuppressedWarningsFixture Class

Namespace: matlab.unittest.fixtures
Superclasses: matlab.unittest.fixtures.Fixture

Fixture for suppressing display of warnings

Description

The matlab.unittest.fixtures.SuppressedWarningsFixture class provides a fixture for suppressing the display of warnings. When the testing framework sets up the fixture, the fixture suppresses the specified warnings. When the framework tears down the fixture, the fixture restores the states of warnings to their original values.

The matlab.unittest.fixtures.SuppressedWarningsFixture class is a handle class.

Creation

Description

example

fixture = matlab.unittest.fixtures.SuppressedWarningsFixture(warnings) creates a fixture for suppressing the display of the specified warnings and sets the Warnings property.

Properties

expand all

Identifiers of the warnings to suppress, returned as a cell array of character vectors. Specify the value of this property during creation of the fixture as a string array, character vector, or cell array of character vectors.

Example: {'MATLAB:MKDIR:DirectoryExists'}

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

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

You can suppress warnings while running a test by using a SuppressedWarningsFixture instance with your test case. For example, suppress the warning that is issued when you try to remove a folder that does not exist from the search path.

The rmpath command issues a warning when you use it to remove a nonexistent folder from the path.

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

Return the identifier of the warning issued by the rmpath command.

[~,identifier] = lastwarn
identifier =

    'MATLAB:rmpath:DirNotFound'

In a file named SuppressedWarningTest.m in your current folder, create a test class that verifies that a call to rmpath runs without any warnings. For the test to pass, call the applyFixture method in your test.

classdef SuppressedWarningTest < matlab.unittest.TestCase
    methods (Test)
        function testWarningFree(testCase)
            import matlab.unittest.fixtures.SuppressedWarningsFixture
            testCase.applyFixture( ...
                SuppressedWarningsFixture("MATLAB:rmpath:DirNotFound"))
            testCase.verifyWarningFree(@() rmpath("nonexistentFolder"))
        end
    end
end

Run the test class. When the Test method invokes the function handle, the fixture suppresses the specified warning. The test passes.

runtests("SuppressedWarningTest");
Running SuppressedWarningTest
.
Done SuppressedWarningTest
__________

Once the test run is complete, the testing framework tears down the fixture and the environment returns to its original state. Therefore, a new call to rmpath with a nonexistent folder results in a warning.

rmpath nonexistentFolder
Warning: "nonexistentFolder" not found in path.

Version History

Introduced in R2013b