onFailure
类: matlab.unittest.TestCase
包: matlab.unittest
动态添加测试失败的诊断
说明
onFailure(
添加在测试失败时执行的诊断。如果测试失败,则测试框架将执行这些诊断。默认情况下,这些诊断在出现验证失败、断言失败、致命断言失败和未捕获的异常时执行。testcase
,failureDiag
)
onFailure(
指示测试框架是否在假设失败时也执行诊断。要在假设失败时也执行诊断,请将 testcase
,failureDiag
,'IncludingAssumptionFailures',tf)tf
设置为 true
。
输入参数
testCase
— 测试用例的实例
matlab.unittest.TestCase
实例
测试用例的实例,指定为 matlab.unittest.TestCase
对象。
failureDiag
— 诊断信息
字符向量 | 字符串数组 | 函数句柄 | 由 matlab.automation.diagnostics.Diagnostic
实例组成的数组
要在出现故障时显示的诊断信息,指定为字符向量、字符串数组、函数句柄或 matlab.automation.diagnostics.Diagnostic
实例数组。
示例: @() disp('Failure Detected')
示例: matlab.unittest.diagnostics.ScreenshotDiagnostic
tf
— 对假设失败作出响应
false
(默认) | true
对假设失败作出响应,指定为 false
(logical
0) 或 true
(logical
1)。默认情况下,此值为 false
,测试框架会在出现验证失败、断言失败、致命断言失败和未捕获的异常时执行诊断。但是,该框架不会在出现假设失败时执行诊断。要在出现假设失败时也执行诊断,请将此值指定为 true
。
示例
添加在测试失败时执行的诊断
在当前文件夹中,创建 SampleOnFailureTest
测试类。
classdef SampleOnFailureTest < matlab.unittest.TestCase methods (TestMethodSetup) function addFailureDiag(testCase) testCase.onFailure('Failure Detected') end end methods (Test) function verificationFailTest(testCase) testCase.onFailure(@()disp(datetime)) testCase.verifyEqual(42,13) end function passingTest(testCase) testCase.assertTrue(true) end function assumptionFailTest(testCase) testCase.assumeEmpty(rand(2)) end function assertionFailTest(testCase) act = randi(100,1,15); floor = randi(100,1,15); f = figure; plot(1:length(act),act,1:length(floor),floor) legend('actual','floor') testCase.addTeardown(@close,f) import matlab.unittest.diagnostics.FigureDiagnostic testCase.onFailure(FigureDiagnostic(f,'Formats','png')) testCase.assertGreaterThan(act,floor) end end end
在命令提示符下运行这些测试。SampleOnFailureTest
类的结果有以下几种:
对于发生验证、断言或致命断言失败的每个测试,显示诊断消息
'Failure Detected'
,因为addFailureDiag
在TestMethodSetup
代码块中调用了onFailure
。verificationFailTest
测试在失败时添加显示当前日期时间的另一个诊断。assumptionFailTest
测试发生假设失败。因此,不会显示'Failure Detected'
消息。assertionFailTest
测试绘制数据图。如果测试失败,测试框架将保存该数据图。
results = runtests('SampleOnFailureTest');
Running SampleOnFailureTest ================================================================================ Verification failed in SampleOnFailureTest/verificationFailTest. --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> The numeric values are not equal using "isequaln". --> Failure table: Actual Expected Error RelativeError ______ ________ _____ ________________ 42 13 29 2.23076923076923 Actual Value: 42 Expected Value: 13 ---------------------- Additional Diagnostic: ---------------------- Failure Detected ---------------------- Additional Diagnostic: ---------------------- 05-Feb-2021 12:13:36 ------------------ Stack Information: ------------------ In C:\work\MyExamples\SampleOnFailureTest.m (SampleOnFailureTest.verificationFailTest) at 10 ================================================================================ .. ================================================================================ SampleOnFailureTest/assumptionFailTest was filtered. ================================================================================ . ================================================================================ Assertion failed in SampleOnFailureTest/assertionFailTest and it did not run to completion. --------------------- Framework Diagnostic: --------------------- assertGreaterThan failed. --> Each element must be greater than each corresponding element of the minimum value array. Failing Indices: 3 8 10 11 13 15 Actual Value: Columns 1 through 13 44 59 29 8 93 66 31 55 84 52 18 97 23 Columns 14 through 15 95 2 Minimum Value (Exclusive): Columns 1 through 13 3 23 32 5 43 1 17 76 62 91 51 33 26 Columns 14 through 15 11 100 ---------------------- Additional Diagnostic: ---------------------- Failure Detected ---------------------- Additional Diagnostic: ---------------------- Figure saved to: --> C:\work\Temp\Figure_20a2887e-85de-4398-8aa4-26ef8d3d921d.png ------------------ Stack Information: ------------------ In C:\work\MyExamples\SampleOnFailureTest.m (SampleOnFailureTest.assertionFailTest) at 27 ================================================================================ . Done SampleOnFailureTest __________ Failure Summary: Name Failed Incomplete Reason(s) ======================================================================================= SampleOnFailureTest/verificationFailTest X Failed by verification. --------------------------------------------------------------------------------------- SampleOnFailureTest/assumptionFailTest X Filtered by assumption. --------------------------------------------------------------------------------------- SampleOnFailureTest/assertionFailTest X X Failed by assertion.
提示
要为类中的每个测试添加一个诊断,请从
TestMethodSetup
代码块中的方法调用onFailure
。
版本历史记录
在 R2017b 中推出
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)