Main Content

verifyWarning

Class: matlab.unittest.qualifications.Verifiable
Package: matlab.unittest.qualifications

Verify function issues specified warning

Description

example

verifyWarning(testCase,actual,identifier) verifies that actual is a function handle that issues the warning specified by identifier.

example

verifyWarning(testCase,actual,identifier,diagnostic) also associates the diagnostic information in diagnostic with the qualification.

example

[output1,...,outputN] = verifyWarning(___) also returns any outputs produced when the function handle is invoked. You can use any of the input argument combinations in the previous syntaxes.

Input Arguments

expand all

Test case object, specified as an instance of the matlab.unittest.qualifications.Verifiable class. Because the matlab.unittest.TestCase class subclasses matlab.unittest.qualifications.Verifiable and inherits its methods, testCase is typically a matlab.unittest.TestCase instance.

Value to test, specified as a value of any data type. Although you can provide a value of any data type, the test fails if actual is not a function handle.

Example: @() myFunction(1,2)

Example: @() mkdir("myFolder")

Warning identifier, specified as a character vector, cell array of character vectors, or string array.

Example: "MATLAB:MKDIR:DirectoryExists"

Example: ["MyComponent:FirstID" "MyComponent:SecondID"]

Diagnostic information to display when the qualification passes or fails, specified as a string array, character array, function handle, or array of matlab.unittest.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework might display diagnostics when the qualification passes or fails. By default, the framework displays diagnostics only when the qualification fails. You can override the default behavior by customizing the test runner. For example, use a DiagnosticsOutputPlugin instance to display both failing and passing event diagnostics.

Example: "My Custom Diagnostic"

Example: @dir

Attributes

Sealedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Verify that the mkdir function issues a specified warning if it is used to create a folder that already exists.

This example assumes that your current folder has a subfolder named myFolder. Create the subfolder if it does not exist.

if ~isfolder("myFolder")
    mkdir myFolder
end

If you try to create myFolder again, mkdir issues a warning. Return the warning identifier.

mkdir myFolder
Warning: Directory already exists.
[~,identifier] = lastwarn
identifier = 
'MATLAB:MKDIR:DirectoryExists'

Now, create a test case for interactive testing. Verify that if mkdir is called to create an existing folder, it warns and the warning has the identifier "MATLAB:MKDIR:DirectoryExists".

testCase = matlab.unittest.TestCase.forInteractiveUse;
verifyWarning(testCase,@() mkdir("myFolder"),"MATLAB:MKDIR:DirectoryExists")
Verification passed.

Test if the actual value is a function handle that issues a specified warning.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that the warning function issues a warning with the expected identifier.

verifyWarning(testCase,@() warning("SOME:warning:id","Warning!"), ...
    "SOME:warning:id")
Verification passed.

Repeat the test with "OTHER:warning:id" as the expected warning identifier. The test fails.

verifyWarning(testCase,@() warning("SOME:warning:id","Warning!"), ...
    "OTHER:warning:id","Warning identifiers must match.")
Warning: Warning! 
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Warning identifiers must match.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyWarning failed.
    --> The function handle did not issue the expected warning(s).
        
        Actual Warning(s):
            --> 'SOME:warning:id'
        Expected Warning(s):
            --> 'OTHER:warning:id'
    
    Evaluated Function:
      function_handle with value:
    
        @()warning("SOME:warning:id","Warning!")
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 21

Test the rand function, and also examine the output of the function. The test fails because rand does not issue any warnings.

r = verifyWarning(testCase,@rand,"SOME:warning:id")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyWarning failed.
    --> The function handle did not issue any warnings.
        
        Expected Warning(s):
            --> 'SOME:warning:id'
    
    Evaluated Function:
      function_handle with value:
    
        @rand
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 27

r =

    0.8147

Verify that the test fails if the actual value is not a function handle.

verifyWarning(testCase,5,"SOME:warning:id")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyWarning failed.
    --> The value must be an instance of the expected type.
        
        Actual Class:
            double
        Expected Type:
            function_handle
    
    Actual Value:
         5
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSpecifiedWarningsExample.m (TestForSpecifiedWarningsExample) at 31

Tips

  • verifyWarning is a convenience method. For example, verifyWarning(testCase,actual,identifier) is functionally equivalent to the following code.

    import matlab.unittest.constraints.IssuesWarnings
    testCase.verifyThat(actual,IssuesWarnings(cellstr(identifier)))
    

    More functionality is available when using the IssuesWarnings constraint directly via verifyThat.

  • Use verification qualifications to produce and record failures without throwing an exception. Since verifications do not throw exceptions, all test content runs to completion even when verification failures occur. Typically, verifications are the primary qualification for a unit test, since they typically do not require an early exit from the test. Use other qualification types to test for violation of preconditions or incorrect test setup:

    • Use assumption qualifications to ensure that the test environment meets preconditions that otherwise do not result in a test failure. Assumption failures result in filtered tests, and the testing framework marks the tests as Incomplete. For more information, see matlab.unittest.qualifications.Assumable.

    • Use assertion qualifications when the failure condition invalidates the remainder of the current test content, but does not prevent proper execution of subsequent tests. A failure at the assertion point renders the current test as Failed and Incomplete. For more information, see matlab.unittest.qualifications.Assertable.

    • Use fatal assertion qualifications to abort the test session upon failure. These qualifications are useful when the failure is so fundamental that continuing testing does not make sense. Fatal assertion qualifications are also useful when fixture teardown does not restore the environment state correctly, and aborting testing and starting a fresh session is preferable. For more information, see matlab.unittest.qualifications.FatalAssertable.

Introduced in R2013a