matlab.unittest.plugins.StopOnFailuresPlugin 类
包: matlab.unittest.plugins
超类: matlab.unittest.plugins.TestRunnerPlugin
在测试失败时进行调试的插件
描述
matlab.unittest.plugins.StopOnFailuresPlugin
类定义用于调试测试失败的插件。当具有 StopOnFailuresPlugin
的测试运行程序遇到验证失败或未捕获的错误时,它会暂停测试执行,并将 MATLAB® 置于调试模式。然后,您可以使用 MATLAB 调试命令,例如 dbstep
、dbcont
和 dbquit
,来调查测试失败或出错的原因。
matlab.unittest.plugins.StopOnFailuresPlugin
类是 handle
类。
创建对象
描述
p = matlab.unittest.plugins.StopOnFailuresPlugin
构造在测试失败时进行调试的插件。
p = matlab.unittest.plugins.StopOnFailuresPlugin("IncludingAssumptionFailures",tf)
将 IncludeAssumptionFailures
属性设置为 tf
。使用此语法指示是否响应假设失败。默认情况下,StopOnFailuresPlugin
仅响应未捕获的错误、验证失败、断言失败和致命断言失败。然而,当 IncludingAssumptionFailures
指定为 true
时,插件还会响应假设失败。
属性
IncludeAssumptionFailures
— 是否响应假设失败
false
或 0
(默认) | true
或 1
是否响应假设失败,指定为数值或逻辑值 0
(false
) 或 1
(true
),并存储为逻辑值。如果值是 true
,则插件响应假设失败。如果值是 false
,则插件会忽略假设失败。
属性:
GetAccess | public |
SetAccess | private |
示例
调试测试失败
通过向测试运行程序添加 StopOnFailuresPlugin
实例来调查测试失败的原因。
在当前文件夹中,创建 ExampleTest
测试类。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testOne(testCase) % Test fails act = 3.1416; exp = pi; testCase.verifyEqual(act,exp) end function testTwo(testCase) % Test does not complete testCase.assumeEqual(5,4) end end end
在命令提示符下,基于 ExampleTest
创建一个测试套件并运行测试。作为测试类中验证的结果,第一个测试失败,第二个测试未完成。
import matlab.unittest.plugins.StopOnFailuresPlugin suite = testsuite("ExampleTest"); runner = testrunner("textoutput"); results = runner.run(suite);
Running ExampleTest ================================================================================ Verification failed in ExampleTest/testOne. --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> The numeric values are not equal using "isequaln". --> Failure table: Actual Expected Error RelativeError ______ ________________ ____________________ ____________________ 3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06 Actual Value: 3.141600000000000 Expected Value: 3.141592653589793 ------------------ Stack Information: ------------------ In C:\work\ExampleTest.m (ExampleTest.testOne) at 6 ================================================================================ . ================================================================================ ExampleTest/testTwo was filtered. ================================================================================ . Done ExampleTest __________ Failure Summary: Name Failed Incomplete Reason(s) ================================================================== ExampleTest/testOne X Failed by verification. ------------------------------------------------------------------ ExampleTest/testTwo X Filtered by assumption.
向测试运行程序添加一个 StopOnFailuresPlugin
实例并重新运行测试。在测试运行期间,当失败发生时,MATLAB 在失败的来源处进入调试模式。
runner.addPlugin(StopOnFailuresPlugin) result = runner.run(suite);
Running ExampleTest ================================================================================ Verification failed in ExampleTest/testOne. --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> The numeric values are not equal using "isequaln". --> Failure table: Actual Expected Error RelativeError ______ ________________ ____________________ ____________________ 3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06 Actual Value: 3.141600000000000 Expected Value: 3.141592653589793 ------------------ Stack Information: ------------------ In C:\work\ExampleTest.m (ExampleTest.testOne) at 6 ================================================================================ Test execution paused due to failure. To terminate the test run, use dbquit. To continue, use dbcont.
通过使用 whos
函数检查工作区中的变量,调查测试失败的原因。
whos
Name Size Bytes Class Attributes act 1x1 8 double exp 1x1 8 double testCase 1x1 8 ExampleTest
尝试以 100*eps
的相对容差重新运行失败的测试。即使具有指定的容差,测试也失败。
testCase.verifyEqual(act,exp,"RelTol",100*eps)
================================================================================ Verification failed in ExampleTest/testOne. --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> The numeric values are not equal using "isequaln". --> The error was not within relative tolerance. --> Failure table: Actual Expected Error RelativeError RelativeTolerance ______ ________________ ____________________ ____________________ ____________________ 3.1416 3.14159265358979 7.34641020683213e-06 2.33843499679617e-06 2.22044604925031e-14 Actual Value: 3.141600000000000 Expected Value: 3.141592653589793 ------------------ Stack Information: ------------------ In C:\work\ExampleTest.m (ExampleTest.testOne) at 6 ================================================================================
尝试以 0.001
的绝对容差重新运行失败的测试。失败的测试以绝对容差通过。
testCase.verifyEqual(act,exp,"AbsTol",0.001)
使用 dbquit
结束测试运行。您也可以使用 dbcont
来退出调试模式并运行其余的测试。
dbquit
要针对按假设失败的测试(例如 ExampleTest
类中的 testTwo
)进入调试模式,请在创建插件时将 IncludingAssumptionFailures
指定为 true
。(确保只向测试运行程序添加一个 StopOnFailuresPlugin
实例。添加多个 StopOnFailuresPlugin
实例可能导致意外行为。)
当您运行测试时,MATLAB 中的 testOne
和 testTwo
都进入调试模式。
runner.addPlugin(StopOnFailuresPlugin( ... "IncludingAssumptionFailures",true)) result = runner.run(suite);
调试测试中未捕获的错误
使用 StopOnFailuresPlugin
实例调试测试中的错误。
在当前文件夹中,创建 ExampleTest
测试类。通过在 testTwo
方法中包含无效的函数调用,将错误注入您的测试中。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testOne(testCase) % Test passes act = round(pi); exp = 3; testCase.verifyEqual(act,exp) end function testTwo(testCase) % Test throws an error act = cosine(0); % Invalid function call exp = 1; testCase.verifyEqual(act,exp) end end end
在命令提示符下,基于 ExampleTest
创建一个测试套件并运行测试。由于存在该错误,第二次测试失败并且未完成。
import matlab.unittest.plugins.StopOnFailuresPlugin suite = testsuite("ExampleTest"); runner = testrunner("textoutput"); results = runner.run(suite);
Running ExampleTest . ================================================================================ Error occurred in ExampleTest/testTwo and it did not run to completion. --------- Error ID: --------- 'MATLAB:UndefinedFunction' -------------- Error Details: -------------- Undefined function 'cosine' for input arguments of type 'double'. Error in ExampleTest/testTwo (line 9) act = cosine(0); % Invalid function call ================================================================================ . Done ExampleTest __________ Failure Summary: Name Failed Incomplete Reason(s) ==================================================== ExampleTest/testTwo X X Errored.
向运行程序添加一个 StopOnFailuresPlugin
实例并重新运行测试。在测试运行期间,当引发错误时,MATLAB 在错误源处进入调试模式。
runner.addPlugin(StopOnFailuresPlugin) result = runner.run(suite);
Running ExampleTest .9 act = cosine(0); % Invalid function call K>>
通过使用正确的函数名称(即 cos
)来修复错误。当您重新运行测试时,它们两个都通过。
版本历史记录
在 R2013b 中推出R2022a: 调试测试中未捕获的错误
当具有 StopOnFailuresPlugin
实例的测试运行程序遇到未捕获的错误时,MATLAB 会在错误源处进入调试模式,并允许您使用调试命令来调查错误的原因。
在以前的版本中,虽然插件会停止测试运行来报告错误,但调试功能将受到限制,因为错误会破坏堆栈。
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)