verifyFail
类: matlab.unittest.qualifications.Verifiable
命名空间: matlab.unittest.qualifications
生成无条件的确认失败
说明
verifyFail(
还将 testCase
,diagnostic
)diagnostic
中的诊断信息与鉴定相关联。
输入参数
testCase
— 测试用例
matlab.unittest.qualifications.Verifiable
对象
测试用例,指定为 matlab.unittest.qualifications.Verifiable
对象。由于 matlab.unittest.TestCase
类会子类化 matlab.unittest.qualifications.Verifiable
并继承其方法,因此 testCase
通常是 matlab.unittest.TestCase
对象。
diagnostic
— 要显示的诊断信息
字符串数组 | 字符数组 | 函数句柄 | matlab.automation.diagnostics.Diagnostic
对象数组
当鉴定通过或失败时显示的诊断信息,指定为字符串数组、字符数组、函数句柄或 matlab.automation.diagnostics.Diagnostic
对象数组。
根据测试运行器的配置,测试框架可能会在鉴定通过或失败时显示诊断信息。默认情况下,框架仅在鉴定失败时显示诊断信息。您可以通过自定义测试运行器来覆盖默认行为。例如,使用 DiagnosticsOutputPlugin
实例来显示失败和通过事件诊断信息。
示例: "My Custom Diagnostic"
示例: @dir
示例
创建占位符以测试新功能
如果您无法编写一项新功能所需的所有测试,请使用无条件测试失败作为未实现测试的占位符。失败会提醒您需要为您的功能开发的测试。
在当前文件夹的一个文件中创建 FeatureTest
类。使用 verifyFail
方法添加两个占位符来测试新功能。
classdef FeatureTest < matlab.unittest.TestCase methods (Test) function defaultBehavior(testCase) testCase.verifyFail end function otherBehavior(testCase) testCase.verifyFail("Add code to test nondefault behavior.") end end end
如果您运行测试,它们都会无条件失败。
runtests("FeatureTest")
Running FeatureTest ================================================================================ Verification failed in FeatureTest/defaultBehavior. ------------------ Stack Information: ------------------ In C:\work\FeatureTest.m (FeatureTest.defaultBehavior) at 4 ================================================================================ . ================================================================================ Verification failed in FeatureTest/otherBehavior. ---------------- Test Diagnostic: ---------------- Add code to test nondefault behavior. ------------------ Stack Information: ------------------ In C:\work\FeatureTest.m (FeatureTest.otherBehavior) at 7 ================================================================================ . Done FeatureTest __________ Failure Summary: Name Failed Incomplete Reason(s) ========================================================================== FeatureTest/defaultBehavior X Failed by verification. -------------------------------------------------------------------------- FeatureTest/otherBehavior X Failed by verification. ans = 1×2 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 0 Passed, 2 Failed, 0 Incomplete. 0.087462 seconds testing time.
回调运行时产生失败
您可以使用 verifyFail
来确保一段代码不会在某些条件下运行。例如,通过在回调方法中调用 verifyFail
,在不希望运行该回调时尝试运行它会导致验证失败。
在当前文件夹内的一个文件中,创建一个具有事件的句柄类。
classdef MyHandle < handle events SomethingHappened end end
在当前文件夹中,创建 ListenerTest
类。添加代码以创建一个事件源、该事件的侦听程序和充当侦听程序回调的辅助方法。然后,添加两个 Test
方法来测试事件触发时的回调行为。
classdef ListenerTest < matlab.unittest.TestCase properties Source Listener end methods (TestMethodSetup) function setup(testCase) % Create the event source testCase.Source = MyHandle; % Add a listener to test execution of the callback code testCase.Listener = testCase.Source.addlistener( ... "SomethingHappened",@testCase.forbiddenCallback); % Remove the listener after the test testCase.addTeardown(@delete,testCase.Listener) end end methods (Test) function passingTest(testCase) % Disable the listener testCase.Listener.Enabled = false; testCase.Source.notify("SomethingHappened") % Callback does not run end function failingTest(testCase) % The listener is enabled by default testCase.Source.notify("SomethingHappened") % Callback runs end end methods function forbiddenCallback(testCase,~,~) % Test fails unconditionally testCase.verifyFail("This callback must not run!") end end end
运行测试。passingTest
会禁用该侦听程序,然后触发事件。因此,该回调不会运行,测试将通过。但是,当 failingTest
触发事件时,forbiddenCallback
会运行,导致由 verifyFail
产生的失败。
runtests("ListenerTest")
Running ListenerTest . ================================================================================ Verification failed in ListenerTest/failingTest. ---------------- Test Diagnostic: ---------------- This callback must not run! ------------------ Stack Information: ------------------ In C:\work\ListenerTest.m (ListenerTest.forbiddenCallback) at 34 In C:\work\ListenerTest.m (@(varargin)testCase.forbiddenCallback(varargin{:})) at 13 In C:\work\ListenerTest.m (ListenerTest.failingTest) at 27 ================================================================================ . Done ListenerTest __________ Failure Summary: Name Failed Incomplete Reason(s) ======================================================================= ListenerTest/failingTest X Failed by verification. ans = 1×2 TestResult array with properties: Name Passed Failed Incomplete Duration Details Totals: 1 Passed, 1 Failed, 0 Incomplete. 0.20956 seconds testing time.
提示
使用此确认在不引发异常的条件下生成和记录失败。由于确认不会引发异常,因此即使出现确认失败的情形,依然会完成所有的测试内容。通常,确认指的是对单元测试的主要鉴定,因为这些确认一般不要求提前从测试中退出。使用其他鉴定类型来测试是否违反先决条件或测试安装是否正确:
使用断言鉴定确保测试环境满足无论如何都不会导致测试失败的先决条件。假设失败会生成已过滤的测试,且测试框架会将测试设为
Incomplete
。有关详细信息,请参阅matlab.unittest.qualifications.Assumable
。当失败条件导致当前测试内容的剩余部分都失效,但不会阻止后续测试正确执行时,使用断言鉴定。断言点处的失败会将当前测试展现为
Failed
和Incomplete
。有关详细信息,请参阅matlab.unittest.qualifications.Assertable
。使用致命断言鉴定在失败时中止测试会话。当失败涉及根本以致继续测试已经没有意义时,这些鉴定非常有用。当脚手架拆解不能正确还原环境状态,适合中止测试并启动一个新会话时,致命断言也很有用。有关详细信息,请参阅
matlab.unittest.qualifications.FatalAssertable
。
版本历史记录
在 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)