Main Content

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

reportFinalizedResult

类: matlab.unittest.plugins.TestRunnerPlugin
包: matlab.unittest.plugins

为最终化测试结果生成报告

说明

示例

reportFinalizedResult(plugin,pluginData) 为最终化测试结果生成报告。当不再有验证可能修改测试结果时,表示测试结果是最终化的。TestRunner 可能会修改以前运行的测试结果,例如在执行 TestClassTeardown 方法中的代码时,或在拆解共享测试脚手架时。

建议使用可覆盖 reportFinalizedResult 方法的插件以对测试结果进行即时报告或内联报告。如果实现此方法,TestRunner 可在结果最终化后立即报告结果。这样,该插件可在测试套件仍在运行时报告测试结果,而不必等到整个套件完成。测试框架可以在 TestRunnerPluginrunTestSuiterunTestClassrunTest 方法的作用域内计算 reportFinalizedResult

输入参数

全部展开

插件对象,指定为 matlab.unittest.plugins.TestRunnerPlugin 类的实例。

最终化测试的信息,指定为 matlab.unittest.plugins.plugindata.FinalizedResultPluginData 类的实例。测试框架使用此信息来描述该插件的测试内容。

示例

全部展开

使用 reportFinalizedResult 方法显示每个 Test 元素的状态。

在当前文件夹中创建插件文件 ExamplePlugin.m

classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin
    methods (Access = protected)
        function reportFinalizedResult(plugin, pluginData)
            thisResult = pluginData.TestResult;
            if thisResult.Passed
                status = 'PASSED';
            elseif thisResult.Failed
                status = 'FAILED';
            elseif thisResult.Incomplete
                status = 'SKIPPED';
            end
            fprintf('%s: %s in %f seconds.\n',thisResult.Name,...
                status,thisResult.Duration)
            reportFinalizedResult@...
                matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData);
        end
    end
end

在当前文件夹中创建测试类文件 ExampleTest.m

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)
            testCase.assertGreaterThan(5,1)
        end
        function testTwo(testCase)
            wrongAnswer = 'wrong';
            testCase.verifyEmpty(wrongAnswer,'Not Empty')
            testCase.verifyClass(wrongAnswer,'double','Not double')
        end
        function testThree(testCase)
            testCase.assumeEqual(7*2,13,'Values not equal')
        end
        function testFour(testCase)
            testCase.verifyEqual(3+2,5);
        end
    end
end

在命令提示符下,创建测试套件,将该插件添加到 TestRunner,并运行测试。

import matlab.unittest.TestSuite
import matlab.unittest.TestRunner

suite = TestSuite.fromClass(?ExampleTest);
runner = TestRunner.withNoPlugins;
runner.addPlugin(ExamplePlugin)
result = runner.run(suite);
ExampleTest/testOne: PASSED in 0.002216 seconds.
ExampleTest/testTwo: FAILED in 0.006105 seconds.
ExampleTest/testThree: SKIPPED in 0.007458 seconds.
ExampleTest/testFour: PASSED in 0.004865 seconds.

版本历史记录

在 R2015b 中推出