matlab.unittest.qualifications.Verifiable.verifyError - Verify function throws specified exception - MATLAB (original) (raw)

Class: matlab.unittest.qualifications.Verifiable
Namespace: matlab.unittest.qualifications

Verify function throws specified exception

Syntax

Description

verifyError([testCase](#bt00p7j-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F28928f0b-a6fa-4b51-86bd-5e97264d00cd),[identifier](#mw%5F5a5f04a7-0353-4941-a016-3392ae83ff90)) verifies that actual is a function handle that throws the exception specified by identifier.

example

verifyError([testCase](#bt00p7j-1%5Fsep%5Fmw%5F8d5e73c7-bacb-46a7-a1c1-f24af91e6c03),[actual](#mw%5F28928f0b-a6fa-4b51-86bd-5e97264d00cd),[identifier](#mw%5F5a5f04a7-0353-4941-a016-3392ae83ff90),[diagnostic](#mw%5F210bc202-c83b-494a-859a-6428f26e8580)) 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.

example

Input Arguments

expand all

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

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, ormatlab.metadata.Class instance.

If identifier is a matlab.metadata.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.automation.diagnostics.Diagnostic objects.

Depending on the test runner configuration, the testing framework can 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

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")

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")

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

Version History

Introduced in R2013a