onFailure
类: matlab.unittest.fixtures.Fixture
包: matlab.unittest.fixtures
动态添加在脚手架设置和拆解过程中出现故障时执行的诊断
说明
onFailure(
添加可在脚手架设置和拆解例程中出现故障时执行的诊断。如果测试框架遇到故障,则会执行这些诊断。默认情况下,这些诊断在出现验证失败、断言失败、致命断言失败和未捕获的异常时执行。 fixture
,failureDiag
)
onFailure(
指示测试框架是否在假设失败时也执行诊断。要在假设失败时也执行诊断,请将 fixture
,failureDiag
,'IncludingAssumptionFailures',tf)tf
设置为 true
。
输入参数
fixture
— 脚手架实例
matlab.unittest.fixtures.Fixture
实例
脚手架实例,指定为 matlab.unittest.fixtures.Fixture
。
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
。
示例
添加针对脚手架故障的诊断
在您的当前工作文件夹中创建 FormatHexFixture
脚手架。测试框架在脚手架设置期间遇到断言失败,并在失败时显示诊断消息。
classdef FormatHexFixture < matlab.unittest.fixtures.Fixture properties (Access=private) OriginalFormat end methods function setup(fixture) fixture.OriginalFormat = format().NumericFormat; format("hex") fixture.addTeardown(@format,fixture.OriginalFormat) fixture.onFailure(['Failure detected in ' mfilename("class")]) fixture.assertEqual(format().NumericFormat,"short") end end end
在您的当前工作文件夹中创建下面的测试类 SampleTest.m
。
classdef SampleTest < matlab.unittest.TestCase methods (Test) function test1(testCase) testCase.applyFixture(FormatHexFixture); actStr = getColumnForDisplay([1;2;3], 'Small Integers'); expStr = ['Small Integers ' '3ff0000000000000' '4000000000000000' '4008000000000000']; testCase.verifyEqual(actStr, expStr) end end end function str = getColumnForDisplay(values, title) elements = cell(numel(values)+1, 1); elements{1} = title; for idx = 1:numel(values) elements{idx+1} = displayNumber(values(idx)); end str = char(elements); end function str = displayNumber(n) str = strtrim(evalc('disp(n);')); end
运行测试。请注意,onFailure
诊断显示在“Additional Diagnostics”标题下。
results = runtests('SampleTest');
Running SampleTest ================================================================================ Assertion failed in SampleTest/test1 and it did not run to completion. --------------------- Framework Diagnostic: --------------------- assertEqual failed. --> The strings are not equal. Actual Value: "hex" Expected Value: "short" ---------------------- Additional Diagnostic: ---------------------- Failure detected in FormatHexFixture ------------------ Stack Information: ------------------ In C:\work\FormatHexFixture.m (FormatHexFixture.setup) at 12 ================================================================================ . Done SampleTest __________ Failure Summary: Name Failed Incomplete Reason(s) ============================================================ SampleTest/test1 X X Failed by assertion.
版本历史记录
在 R2017b 中推出R2019a: Access
属性已更改为 protected
onFailure
方法的 Access
属性已从 public
更改为 protected
。此更改将 onFailure
的使用限制在派生自 matlab.unittest.fixtures.Fixture
的类。
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)