matlab.unittest.constraints.IssuesWarnings 类
命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.Constraint
测试函数是否发出指定的警告
描述
matlab.unittest.constraints.IssuesWarnings 类提供一个约束来测试函数句柄是否发出指定的警告。
matlab.unittest.constraints.IssuesWarnings 类是一个 handle 类。
创建对象
描述
c = matlab.unittest.constraints.IssuesWarnings( 创建一个约束来测试函数句柄是否使用指定的标识符发出警告。如果实际值是函数句柄,且当测试框架调用它时发出指定警告,则满足约束。默认情况下,约束会忽略警告出现的次数、出现的顺序以及函数句柄是否发出任何未指定的警告。identifiers)
c = matlab.unittest.constraints.IssuesWarnings( 使用一个或多个名称-值参量指定选项。例如,identifiers,Name,Value)c = matlab.unittest.constraints.IssuesWarnings(identifiers,"WhenNargoutIs",2) 创建一个约束来测试某函数句柄在用两个输出参量调用时是否会引发指定的错误。
输入参量
警告标识符,指定为字符串数组或字符向量元胞数组。
此参量设置 ExpectedWarnings 属性。
示例: "MATLAB:MKDIR:DirectoryExists"
示例: ["MyComponent:FirstID" "MyComponent:SecondID"]
名称-值参数
以 Name1=Value1,...,NameN=ValueN 的形式指定可选参量对组,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: c = matlab.unittest.constraints.IssuesWarnings(identifiers,WhenNargoutIs=2)
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。
示例: c = matlab.unittest.constraints.IssuesWarnings(identifiers,"WhenNargoutIs",2)
调用函数句柄时约束请求的输出数目,指定为非负整数标量。
此参量设置 Nargout 属性。
数据类型: double | single | int8 | int16 | int32 | int64 | uint8 | uint16 | uint32 | uint64
比较发出警告和预期警告时考虑预期警告集的选项,指定为数值或逻辑值 0 (false) 或 1 (true)。如果值为 true,并且函数句柄发出 identifiers 中未列出的任何警告,则不满足约束。默认情况下,约束会忽略函数句柄发出的任何意外警告。
此参量设置 RespectSet 属性。
比较发出警告和预期警告时考虑计数的选项,指定为数值或逻辑值 0 (false) 或 1 (true)。如果值为 true,并且函数句柄发出预期警告的次数不同于警告在 identifiers 中列出的次数,则不满足约束。默认情况下,约束会忽略警告出现的次数。
此参量设置 RespectCount 属性。
比较发出警告和预期警告时考虑顺序的选项,指定为数值或逻辑值 0 (false) 或 1 (true)。如果值为 true,并且函数句柄发出警告的顺序与它们在 identifiers 中列出的顺序不匹配,则不满足约束。默认情况下,约束会忽略警告出现的顺序。
该约束通过将给定警告配置文件删减为没有任何重复相邻警告的配置文件来确定其顺序。例如,在测试顺序时,警告配置文件 {id1,id1,id2,id3,id3,id3,id1,id1} 变为 {id1,id2,id3,id1}。此外,在测试顺序时,该约束会忽略 identifiers 中未列出的任何发出警告。
此参量设置 RespectOrder 属性。
比较发出警告和预期警告时测试是否存在精确匹配项的选项,指定为数值或逻辑值 0 (false) 或 1 (true)。如果值为 true,并且发出警告和预期警告配置文件不完全相同,则不满足约束。默认情况下,约束不执行精确比较。
此参量设置 Exact 属性。
属性
预期警告标识符,以字符向量元胞数组形式返回。在创建约束的过程中指定此属性的值。
此属性由 identifiers 输入参量设置。
属性:
GetAccess | public |
SetAccess | immutable |
当测试框架调用函数句柄时函数句柄产生的输出,以元胞数组形式返回。
属性:
GetAccess | public |
SetAccess | private |
比较发出警告和预期警告时考虑预期警告集的选项,以 logical 数据类型的 0 或 1 形式返回。默认情况下,约束会忽略函数句柄发出的任何意外警告。
此属性由 RespectingSet 名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
比较发出警告和预期警告时考虑计数的选项,以 logical 数据类型的 0 或 1 形式返回。默认情况下,约束会忽略警告出现的次数。
此属性由 RespectingCount 名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
比较发出警告和预期警告时考虑顺序的选项,以 logical 数据类型的 0 或 1 形式返回。默认情况下,约束会忽略警告出现的顺序。
此属性由 RespectingOrder 名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
比较发出警告和预期警告时测试是否存在精确匹配项的选项,以 logical 数据类型的 0 或 1 形式返回。默认情况下,约束不执行精确比较。
此属性由 Exactly 名称-值参量设置。
属性:
GetAccess | public |
SetAccess | private |
示例
测试 actual 值是否为发出指定警告的函数句柄。
导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IssuesWarnings
创建一个供交互测试的测试用例。
testCase = TestCase.forInteractiveUse;
此示例假设您的当前文件夹中存在名为 myFolder 的子文件夹。如果该子文件夹不存在,请创建它。
[~,~] = mkdir("myFolder")mkdir 函数在用于创建已存在的文件夹时发出警告。返回警告标识符。
mkdir myFolder
[~,id] = lastwarnWarning: Directory already exists.
id =
'MATLAB:MKDIR:DirectoryExists'验证如果调用 mkdir 来创建现有文件夹,它会发出警告,并且警告具有标识符 "MATLAB:MKDIR:DirectoryExists"。
testCase.verifyThat(@() mkdir("myFolder"), ... IssuesWarnings("MATLAB:MKDIR:DirectoryExists"))
Verification passed.
测试不发出任何警告的函数句柄。测试失败。
testCase.verifyThat(@dir,IssuesWarnings("MATLAB:MKDIR:DirectoryExists")). .. myFolder
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IssuesWarnings failed.
--> The function handle did not issue a correct warning profile.
The expected warning profile ignores:
Set
Count
Order
--> The function handle did not issue any warnings.
Expected Warning Profile:
--> 'MATLAB:MKDIR:DirectoryExists'
Evaluated Function:
function_handle with value:
@dir确认如果实际值不是函数句柄,则不满足该约束。
testCase.verifyThat(5,IssuesWarnings("MATLAB:MKDIR:DirectoryExists"))Verification failed.
---------------------
Framework Diagnostic:
---------------------
IssuesWarnings failed.
--> The value must be an instance of the expected type.
Actual Class:
double
Expected Type:
function_handle
Actual Value:
5使用 IssuesWarnings 约束测试可以发出多个警告的函数句柄。
在当前文件夹的一个文件中创建 myfun 函数。该函数接受一个警告标识符数组作为输入,并发出与指定标识符对应的警告。
function myfun(id) arguments id {mustBeText,mustBeNonempty} end for i = 1:numel(id) warning(string(id(i)),"Warning!") end end
导入此示例中使用的类。
import matlab.unittest.TestCase import matlab.unittest.constraints.IssuesWarnings
要使用 IssuesWarnings 约束进行试验,请为交互式测试创建几个警告标识符和一个测试用例。
id1 = "MyComponent:FirstID"; id2 = "MyComponent:SecondID"; id3 = "MyComponent:ThirdID"; testCase = TestCase.forInteractiveUse;
验证 myfun 在使用特定标识符调用时发出警告。
testCase.verifyThat(@() myfun(id1),IssuesWarnings(id1))
Verification passed.
验证在 myfun 发出意外警告时是否不满足约束。
testCase.verifyThat(@() myfun(id1),IssuesWarnings(id2))
Warning: Warning!
...
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IssuesWarnings failed.
--> The function handle did not issue a correct warning profile.
The expected warning profile ignores:
Set
Count
Order
--> The function handle did not issue the correct warning(s).
Missing Warning(s):
--> 'MyComponent:SecondID'
Actual Warning Profile:
--> 'MyComponent:FirstID'
Expected Warning Profile:
--> 'MyComponent:SecondID'
Evaluated Function:
function_handle with value:
@()myfun(id1)当函数句柄发出几个警告时,对其进行测试。此测试通过,因为 myfun 发出两个预期警告。
testCase.verifyThat(@() myfun([id1 id3 id2 id1]), ...
IssuesWarnings([id2 id1]))Verification passed.
在考虑预期警告集的情况下再次测试。测试失败,因为除了预期警告之外,myfun 还发出了 id3 警告。
testCase.verifyThat(@() myfun([id1 id3 id2 id1]), ... IssuesWarnings([id2 id1],"RespectingSet",true))
Warning: Warning!
...
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IssuesWarnings failed.
--> The function handle did not issue a correct warning profile.
The expected warning profile respects:
Set
The expected warning profile ignores:
Count
Order
--> The function handle did not issue the correct warning(s).
Extra Warning(s):
--> 'MyComponent:ThirdID'
Actual Warning Profile:
--> 'MyComponent:FirstID'
--> 'MyComponent:ThirdID'
--> 'MyComponent:SecondID'
--> 'MyComponent:FirstID'
Expected Warning Profile:
--> 'MyComponent:SecondID'
--> 'MyComponent:FirstID'
Evaluated Function:
function_handle with value:
@()myfun([id1,id3,id2,id1])测试 myfun 是否在考虑警告计数的情况下发出指定的警告。测试通过,因为函数句柄发出每个预期警告的次数与预期警告配置文件中的次数相同。
testCase.verifyThat(@() myfun([id2 id1 id3 id2]), ... IssuesWarnings([id1 id2 id2],"RespectingCount",true))
Verification passed.
验证 myfun 在考虑警告顺序的情况下是否发出指定的警告。
testCase.verifyThat(@() myfun([id1 id2 id2 id3]), ... IssuesWarnings([id1 id2],"RespectingOrder",true))
Verification passed.
测试 myfun 是否在考虑警告集、计数和顺序的情况下发出指定的警告。测试通过。
testCase.verifyThat(@() myfun([id1 id1 id2 id1]), ... IssuesWarnings([id1 id2 id1 id1], ... "RespectingSet",true,"RespectingCount",true,"RespectingOrder",true))
Verification passed.
在前面的测试中,即使函数句柄在满足约束的情况下考虑警告集、计数和顺序,发出警告配置文件和预期警告配置文件也并不完全相同。如果测试是否存在精确匹配项,则不满足约束。
testCase.verifyThat(@() myfun([id1 id1 id2 id1]), ... IssuesWarnings([id1 id2 id1 id1],"Exactly",true))
Warning: Warning!
...
Verification failed.
---------------------
Framework Diagnostic:
---------------------
IssuesWarnings failed.
--> The function handle did not issue a correct warning profile.
The expected warning profile must match exactly.
--> The function handle did not issue the exact warning profile expected.
Actual Warning Profile:
--> 'MyComponent:FirstID'
--> 'MyComponent:FirstID'
--> 'MyComponent:SecondID'
--> 'MyComponent:FirstID'
Expected Warning Profile:
--> 'MyComponent:FirstID'
--> 'MyComponent:SecondID'
--> 'MyComponent:FirstID'
--> 'MyComponent:FirstID'
Evaluated Function:
function_handle with value:
@()myfun([id1,id1,id2,id1])版本历史记录
在 R2013a 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)