Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.unittest.plugins.DiagnosticsValidationPlugin 类

包: matlab.unittest.plugins

帮助验证诊断代码的插件

描述

DiagnosticsValidationPlugin 创建插件以帮助验证诊断代码。

DiagnosticsValidationPlugin 添加到 TestRunner 以确认用户提供的诊断正确执行。此插件很有用,这是因为通常的测试不会遇到失败条件。失败会导致诊断代码无法执行。如果此诊断代码中存在编程错误,则除非测试失败,否则该错误不明显。但是,由于诊断代码中存在错误,测试过程中的此时该失败条件的诊断信息已经丢失。

使用此插件可无条件评估测试编写者提供的诊断信息,而不管该测试产生通过还是失败条件。此方法可帮助您确认所有诊断代码都不含编程错误。

诊断分析会降低测试性能,并可能生成非常详细的文本输出。在使用此插件进行常规测试前,请注意存在这些影响。

构造

matlab.unittest.plugins.DiagnosticsValidationPlugin 创建插件以帮助验证诊断代码。

matlab.unittest.plugins.DiagnosticsValidationPlugin(stream) 将所有文本输出重定向到输出流 stream。如果您不指定输出流,则该插件使用默认 ToStandardOutput 流。

输入参数

stream

插件定向文本输出的位置,指定为 OutputStream

默认: ToStandardOutput

复制语义

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

示例

全部折叠

在您的工作文件夹中,创建一个包含以下测试类的文件 ExampleTest.m。在此示例中,testThree 方法具有故意错误。该方法应使用 dir 函数的函数句柄作为 FunctionHandleDiagnostic,但 dir 拼写错误。

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)
            % test code
        end
        function testTwo(testCase)
            % test code
        end
        function testThree(testCase)
            % The following should use @dir as a function handle,
            % but there is a typo
            testCase.verifyEqual('myfile','myfile', @dri)
        end
    end
end

ExampleTest.m 中的所有测试都产生通过条件,但诊断中存在一个错误。

在命令提示符下,基于 ExampleTest 类创建测试套件。

import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.DiagnosticsValidationPlugin

suite = TestSuite.fromClass(?ExampleTest);

创建使用文本输出配置的测试运行程序。

runner = TestRunner.withTextOutput;

运行测试。

result1 = runner.run(suite);
Running ExampleTest
...
Done ExampleTest
__________

不显示任何诊断输出,这是因为所有测试都已通过。测试框架在 testThreeFunctionHandleDiagnostic 中不会遇到错误。

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

runner.addPlugin(DiagnosticsValidationPlugin)
result2 = runner.run(suite);
Running ExampleTest
..
------------------------------
Validation of Test Diagnostic:
------------------------------
Error occurred while capturing diagnostics:
Error using evalc
Undefined function or variable 'dri'.

Error in ExampleTest/testThree (line 12)
            testCase.verifyEqual('myfile','myfile', @dri);

.
Done ExampleTest
__________

该框架执行 FunctionHandleDiagnostic 提供的诊断,即使任何测试都不失败也如此。如果不使用此插件,测试框架仅在测试失败时才遇到错误。