Main Content

本页对应的英文页面已更新,但尚未翻译。 若要查看最新内容,请点击此处访问英文页面。

matlab.unittest.plugins.StopOnFailuresPlugin 类

包: matlab.unittest.plugins

在测试失败时进行调试的插件

说明

StopOnFailuresPlugin 类提供用于在测试失败时进行调试的插件。将 StopOnFailuresPlugin 添加到测试运行程序会在其遇到验证失败或未捕获错误时暂停测试执行,并将 MATLAB® 置于调试模式。

如果 StopOnFailuresPlugin 在测试中遇到验证失败或未捕获错误,您可以使用 MATLAB 调试命令(例如 dpbupdbstepdbcontdbquit)研究测试失败的根源。

如果 StopOnFailuresPlugin 在测试中遇到未捕获错误,您不能使用 dbup 将上下文切换到错误源,这是因为该错误会使堆栈中断。

构造

matlab.unittest.plugins.StopOnFailuresPlugin 创建在测试失败时进行调试的插件。

matlab.unittest.plugins.StopOnFailuresPlugin('IncludingAssumptionFailures',tf) 指示是否响应假设失败。默认情况下,StopOnFailuresPlugin 只响应未捕获错误和验证、断言以及致命断言限定错误。但是,当 'IncludingAssumptionFailures' 设置为 true 时,插件还会响应假设失败。

输入参数

全部展开

是否响应假设失败的指示符,指定为逻辑值 falsetrue。当该值为 true 时,测试运行器将响应假设失败。当该值为 false 时,该插件将忽略假设失败。

属性

IncludeAssumptionFailures

当此属性值为 true 时,实例将响应假设失败。当该值为 false 时,实例将忽略假设失败。默认情况下,IncludeAssumptionFailures 属性为 false。要将该属性指定为 true,请在构造实例时使用 IncludingAssumptionFailures 输入。

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

在您的工作文件夹中,创建包含以下测试类的文件 ExampleTest.m

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.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.StopOnFailuresPlugin

suite = TestSuite.fromClass(?ExampleTest);
runner = TestRunner.withTextOutput;

运行测试。

result = runner.run(suite);
Running ExampleTest

================================================================================
Verification failed in ExampleTest/testOne.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Actual        Expected               Error               RelativeError    
                ______    ________________    ____________________    ____________________
            
                3.1416    3.14159265358979    7.34641020683213e-06    2.33843499679617e-06
    
    Actual double:
           3.141600000000000
    Expected double:
           3.141592653589793

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================
.
================================================================================
ExampleTest/testTwo was filtered.
    Details
================================================================================
.
Done ExampleTest
__________

Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
    ==================================================================
     ExampleTest/testOne    X                 Failed by verification.
    ------------------------------------------------------------------
     ExampleTest/testTwo              X       Filtered by assumption.

作为测试类中验证的结果,第一个测试失败,第二个测试未完成。

StopOnFailuresPlugin 添加到运行程序并运行测试。

runner.addPlugin(StopOnFailuresPlugin)
result = runner.run(suite);
Running ExampleTest
Test execution paused due to failure. Either click here or execute DBUP to shift context to its source: line 6 of "C:\work\ExampleTest.m".

在测试执行期间,当发生失败时,MATLAB 进入调试模式。

点击带超链接的单词 'here' 可将调试上下文切换到您的工作源。如果需要,将该命令行窗口设置为当前窗口。

In workspace belonging to ExampleTest>ExampleTest.testOne at 6

检查工作区中的变量。

whos
  Name          Size            Bytes  Class           Attributes

  act           1x1                 8  double                    
  exp           1x1                 8  double                    
  testCase      1x1               112  ExampleTest               

现在,您可以研究测试失败的原因。

例如,查看在指定 100*eps 的相对误差时测试是否通过。

testCase.verifyEqual(act,exp,'RelTol',100*eps)
Verification failed in ExampleTest/testOne.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The 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 double:
           3.141600000000000
    Expected double:
           3.141592653589793

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================

即使具有指定的容差,测试也失败。

退出调试模式。

dbquit
================================================================================
Verification failed in ExampleTest/testOne.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Actual        Expected               Error               RelativeError    
                ______    ________________    ____________________    ____________________
            
                3.1416    3.14159265358979    7.34641020683213e-06    2.33843499679617e-06
    
    Actual double:
           3.141600000000000
    Expected double:
           3.141592653589793

    ------------------
    Stack Information:
    ------------------
    In C:\work\ExampleTest.m (ExampleTest.testOne) at 6
================================================================================
.
================================================================================
ExampleTest/testTwo was filtered.
    Details
================================================================================
.
Done ExampleTest
__________

Failure Summary:

     Name                 Failed  Incomplete  Reason(s)
    ==================================================================
     ExampleTest/testOne    X                 Failed by verification.
    ------------------------------------------------------------------
     ExampleTest/testTwo              X       Filtered by assumption.

要针对按假设失败的测试(例如 ExampleTest 类中的 testTwo)进入调试模式,请包括插件的 'IncludingAssumptionFailures' 选项。

runner = TestRunner.withTextOutput;
runner.addPlugin(StopOnFailuresPlugin(...
    'IncludingAssumptionFailures', true))

如果您运行测试运行程序,则同时针对 testOnetestTwo 进入调试模式。

在 R2013b 中推出