Main Content

本页的翻译已过时。点击此处可查看最新英文版本。

onFailure

类: matlab.unittest.TestCase
包: matlab.unittest

动态添加检测故障的诊断

说明

示例

onFailure(testcase,failureDiag) 添加在测试失败时执行的诊断。如果测试失败,测试框架将执行这些诊断。默认情况下,这些诊断在出现验证失败、断言失败、致命断言失败和未捕获的异常时执行。

onFailure(testcase,failureDiag,'IncludingAssumptionFailures',tf) 指示测试框架是否在假设失败时也执行诊断。要在假设失败时也执行诊断,请将 tf 设置为 true

输入参数

全部展开

测试用例的实例,指定为 matlab.unittest.TestCase

要在出现故障时显示的诊断信息,指定为字符向量、字符串数组、函数句柄或 matlab.unittest.diagnostics.Diagnostic 实例数组。

示例: @() disp('Failure Detected')

示例: matlab.unittest.diagnostics.ScreenshotDiagnostic

对假设失败作出响应,指定为 false (logical 0) 或 true (logical 1)。默认情况下,此值为 false,测试框架会在出现验证失败、断言失败、致命断言失败和未捕获的异常时执行诊断。但是,测试框架不会在出现假设失败时执行诊断。要在出现假设失败时也执行诊断,请将此值指定为 true

示例

全部展开

在您的当前工作文件夹中创建下面的测试类 SampleOnFailureTest.m

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; hold on;
            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',因为 addFailureDiagTestMethodSetup 块的函数中调用了 onFailure

  • verificationFailTest 测试在失败时添加显示当前日期和时间的另一个诊断。

  • assumptionFailTest 测试发生假设失败。因此不显示 'Failure Detected' 消息。

  • assertionFailTest 测试绘制数据图。如果测试失败,测试框架将保存该数据图。

results = runtests('SampleOnFailureTest');
Running SampleOnFailureTest

================================================================================
Verification failed in SampleOnFailureTest/verificationFailTest.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The 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

    ------------------
    Stack Information:
    ------------------
    In C:\work\SampleOnFailureTest.m (SampleOnFailureTest.verificationFailTest) at 10
================================================================================
..
================================================================================
SampleOnFailureTest/assumptionFailTest was filtered.
Details
================================================================================
.
================================================================================
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:
             1     2     3     4     5     6     9    12    13    14
    
    Actual Value:
        55    68     4    81    75    13    53    33    55    40    42    19    26     3    93
    Minimum Value (Exclusive):
        66    94    17    93    80    58    45    26    76    23     7    77    68    72    65

    ----------------------
    Additional Diagnostic:
    ----------------------
    Failure Detected

    ----------------------
    Additional Diagnostic:
    ----------------------
    Figure saved to:
    --> C:\Temp\070a23db-3903-4abb-9976-2d29ec0a1e38\Figure_c2528bb2-6d72-48d5-a8ed-2a9d5ae7b3ee.png

    ------------------
    Stack Information:
    ------------------
    In C:\work\SampleOnFailureTest.m (SampleOnFailureTest.assertionFailTest) at 28
================================================================================
.
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 中推出