Main Content

verifyError

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

Verify function throws specified exception

Description

example

verifyError(testCase,actual,identifier) verifies that actual is a function handle that throws the exception specified by identifier.

example

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

example

[output1,...,outputN] = verifyError(___) also returns the output values produced by the function handle. Use this syntax to control the number of outputs to request when the function handle is invoked. If the function handle throws an exception, all outputs are displayed as <missing>. 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: @() rmdir("myFolder")

Error identifier, specified as a string scalar, character vector, or meta.class instance.

If identifier is a meta.class instance, then the thrown exception must be an instance of the specified class or one of its subclasses.

Example: "MATLAB:UndefinedFunction"

Example: ?MException

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

Use verifyError to test if a function properly reacts to invalid inputs.

In a file in your current folder, create the add5 function. The function accepts a numeric input and increments it by five. If called with a nonnumeric input, the function throws the exception specified by "add5:InputMustBeNumeric".

function y = add5(x)
% add5 - Increment input by 5
if ~isa(x,"numeric")
    error("add5:InputMustBeNumeric","Input must be numeric.")
end
y = x + 5;
end

Create a test case for interactive testing, and then verify that add5 throws the specified exception if it is called with the input '0'.

testCase = matlab.unittest.TestCase.forInteractiveUse;
verifyError(testCase,@() add5('0'),"add5:InputMustBeNumeric")
Verification passed.

Test if the actual value is a function handle that throws a specified exception.

Create a test case for interactive testing.

testCase = matlab.unittest.TestCase.forInteractiveUse;

Verify that the error function throws an exception with the expected identifier.

verifyError(testCase,@() error("SOME:error:id","Error!"),"SOME:error:id")
Verification passed.

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

verifyError(testCase,@() error("SOME:error:id","Error!"), ...
    "OTHER:error:id","Error identifiers must match.")
Verification failed.
    ----------------
    Test Diagnostic:
    ----------------
    Error identifiers must match.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyError failed.
    --> The function threw the wrong exception.
        
        Actual Exception:
            'SOME:error:id'
        Expected Exception:
            'OTHER:error:id'
    --> Actual Error Report:
            Error using
            VerifyErrorTestForSpecifiedExceptionsExample>@()error("SOME:error:id","Error!")
            (line 20)
            Error!
    
    Evaluated Function:
      function_handle with value:
    
        @()error("SOME:error:id","Error!")
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSpecifiedExceptionsExample.m (TestForSpecifiedExceptionsExample) at 20

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

r = verifyError(testCase,@rand,?MException)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyError failed.
    --> The function did not throw any exception.
        
        Expected Exception:
            ?MException
    
    Evaluated Function:
      function_handle with value:
    
        @rand
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestForSpecifiedExceptionsExample.m (TestForSpecifiedExceptionsExample) at 26

r =

    0.8147

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

verifyError(testCase,5,?MException)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyError 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\TestForSpecifiedExceptionsExample.m (TestForSpecifiedExceptionsExample) at 30

Verify that a function throws a specified exception if it is called with too many outputs.

In a file in your current folder, create the variableNumArguments function that accepts a variable number of inputs and outputs. If the number of outputs is greater than the number of inputs, the function throws an exception. Otherwise, it returns the class of inputs.

function varargout = variableNumArguments(varargin)
if nargout > nargin
    error("variableNumArguments:TooManyOutputs", ...
        "Number of outputs must not exceed the number of inputs.")
end
varargout = cell(1,nargout);
for i = 1:nargout
    varargout{i} = class(varargin{i});
end
end

Create a test case for interactive testing. Then, test variableNumArguments when you provide it with two inputs and request the same number of outputs. The test fails because the function does not throw the specified exception.

testCase = matlab.unittest.TestCase.forInteractiveUse;
[c1,c2] = verifyError(testCase,@() variableNumArguments(1,'2'), ...
    "variableNumArguments:TooManyOutputs")
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyError failed.
    --> The function did not throw any exception.
        
        Expected Exception:
            'variableNumArguments:TooManyOutputs'
    
    Evaluated Function:
      function_handle with value:
    
        @()variableNumArguments(1,'2')
    ------------------
    Stack Information:
    ------------------
    In C:\work\TestFunctionWithVariableNumberOfInputsAndOutputsExample.m (TestFunctionWithVariableNumberOfInputsAndOutputsExample) at 22

c1 =

    'double'


c2 =

    'char'

Verify that if variableNumArguments is called with too many outputs, it throws an exception with the identifier "variableNumArguments:TooManyOutputs".

[c1,c2,c3] = verifyError(testCase,@() variableNumArguments(1,'2'), ...
    "variableNumArguments:TooManyOutputs")
Verification passed.

c1 = 

  missing

    <missing>


c2 = 

  missing

    <missing>


c3 = 

  missing

    <missing>

Tips

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

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

    More functionality is available when using the Throws 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