matlab.unittest.constraints.Throws 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint
测试函数是否引发指定的错误
描述
matlab.unittest.constraints.Throws
类提供一个约束来测试函数句柄是否引发指定的错误。
matlab.unittest.constraints.Throws
类是一个 handle
类。
创建对象
描述
c = matlab.unittest.constraints.Throws(
创建一个约束来测试函数句柄是否引发 identifier
)identifier
指定的错误。如果实际值是在被测试框架调用时引发指定错误的函数句柄,则满足约束。
c = matlab.unittest.constraints.Throws(
使用一个或多个名称-值参量指定选项。例如,identifier
,Name,Value
)c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)
创建一个约束来测试某函数句柄在用两个输出参量调用时是否会引发指定的错误。
输入参量
identifier
— 错误标识符。
字符串标量 | 字符向量 | matlab.metadata.Class
实例
错误标识符,指定为字符串标量、字符向量或 matlab.metadata.Class
实例。
如果 identifier
是 matlab.metadata.Class
实例,且如果引发的错误指定类的实例或其子类之一,则满足约束。
此参量设置 ExpectedException
属性。
示例: "MATLAB:UndefinedFunction"
示例: ?MException
将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: c = matlab.unittest.constraints.Throws(identifier,WhenNargoutIs=2)
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: c = matlab.unittest.constraints.Throws(identifier,"WhenNargoutIs",2)
WhenNargoutIs
— 输出数量
0 (默认) | 非负整数标量
调用函数句柄时约束请求的输出的数目,指定为非负整数标量。
此参量设置 Nargout
属性。
数据类型: double
| single
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
CausedBy
— 必需的错误原因
字符串数组 | 字符向量元胞数组 | 由 matlab.metadata.Class
实例组成的数组
预期错误的必需原因,指定为字符串数组、字符向量元胞数组或由 matlab.metadata.Class
实例组成的数组。如果函数句柄引发错误时缺失任何指定的原因,则不满足约束。
此参量设置 RequiredCauses
属性。
RespectingSet
— 是否遵从一组必需的原因
false
或 0
(默认) | true
或 1
是否遵从一组必需的原因,指定为数值或逻辑值 0
(false
) 或 1
(true
)。如果值为 true
,且预期的错误包含 CausedBy
名称-值参量未指定的原因,则不满足约束。默认情况下,约束对其他原因不敏感。
此参量设置 RespectSet
属性。
属性
ExpectedException
— 预期错误的标识符
字符向量 | matlab.metadata.Class
实例
预期错误的标识符,以字符向量或 matlab.metadata.Class
实例形式返回。
此属性由 identifier
输入参量设置。
属性:
GetAccess | public |
SetAccess | immutable |
Nargout
— 输出数量
0 (默认) | 非负整数标量
RequiredCauses
— 必需的错误原因
字符向量元胞数组 | 由 matlab.metadata.Class
实例组成的数组
预期错误的必需原因,以字符向量元胞数组或由 matlab.metadata.Class
实例组成的数组形式返回。
此属性由 CausedBy
名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
RespectSet
— 是否遵从一组必需的原因
0
(默认) | 1
是否遵从一组必需的原因,以数据类型为 logical
的 0
或 1
形式返回。
此属性由 RespectingSet
名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
示例
测试指定的错误
测试 actual 值是否为引发指定错误的函数句柄。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.Throws
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
验证 error
函数引发具有预期标识符的错误。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws("SOME:error:id"))
Verification passed.
通过将约束传递给 matlab.metadata.Class
实例而不是字符串,再次进行测试。测试通过。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws(?MException))
Verification passed.
如果实际和预期的错误标识符不匹配,则验证测试失败。
testCase.verifyThat(@() error("SOME:error:id","Error!"), ... Throws("OTHER:error:id"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The function threw the wrong exception. Actual Exception: 'SOME:error:id' Expected Exception: 'OTHER:error:id' --> Actual Error Report: Error using @()error("SOME:error:id","Error!") Error! Evaluated Function: function_handle with value: @()error("SOME:error:id","Error!")
测试 rand
函数。测试失败,因为 rand
没有引发任何错误。
testCase.verifyThat(@rand,Throws(?MException))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The function did not throw any exception. Expected Exception: ?MException Evaluated Function: function_handle with value: @rand
disp
函数不返回任何输出。验证用输出参量调用 disp
会引发指定的错误。
testCase.verifyThat(@() disp("Hello World!"), ... Throws("MATLAB:maxlhs","WhenNargoutIs",1))
Verification passed.
验证如果实际值不是函数句柄,则不满足 Throws
约束。
testCase.verifyThat(5,Throws("SOME:error:id"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The value must be an instance of the expected type. Actual Class: double Expected Type: function_handle Actual Value: 5
测试错误原因
使用 Throws
约束测试错误原因。
首先,导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.Throws
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
创建一个 MException
对象 me
,有两个错误原因 cause1
和 cause2
。
me = MException("TOP:id","Top-Level Error!"); cause1 = MException("CAUSE1:id1","First Cause of Error!"); cause2 = MException("CAUSE2:id2","Second Cause of Error!"); me = me.addCause(cause1); me = me.addCause(cause2);
验证当框架引发 cause1
时它是 me
的原因。
testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification passed.
通过将原因指定为 matlab.metadata.Class
实例,再次进行测试。测试通过。如果 me
没有原因,测试将失败。
testCase.verifyThat(@() me.throw,Throws("TOP:id","CausedBy",?MException))
Verification passed.
测试除了 cause1
之外,错误是否没有其他原因。测试失败,因为 cause2
也是预期错误的原因。
testCase.verifyThat(@() me.throw, ... Throws("TOP:id","CausedBy","CAUSE1:id1","RespectingSet",true))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The following causes were unexpectedly found in the exception tree: --> 'CAUSE2:id2' --> Actual Error Report: Error using @()me.throw Top Level Error! Caused by: First Cause of Error! Second Cause of Error! Actual Error Structure: ?MException 'TOP:id' --> ?MException 'CAUSE1:id1' --> ?MException 'CAUSE2:id2' Evaluated Function: function_handle with value: @()me.throw
验证如果指定的原因不属于错误,是否不满足约束。
testCase.verifyThat(@() error("TOP:id","Top-Level Error!"), ... Throws("TOP:id","CausedBy","CAUSE1:id1"))
Verification failed. --------------------- Framework Diagnostic: --------------------- Throws failed. --> The following causes were not found in the exception tree: --> 'CAUSE1:id1' --> Actual Error Report: Error using @()error("TOP:id","Top-Level Error!") Top-Level Error! Actual Error Structure: ?MException 'TOP:id' Evaluated Function: function_handle with value: @()error("TOP:id","Top-Level Error!")
版本历史记录
在 R2013a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)