Main Content

reportFinalizedResult

类: matlab.unittest.plugins.TestRunnerPlugin
命名空间: matlab.unittest.plugins

为最终化测试结果扩展报告

说明

示例

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

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

输入参数

全部展开

插件,指定为 matlab.unittest.plugins.TestRunnerPlugin 对象。

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

属性

Accessprotected

要了解方法的属性,请参阅方法属性

示例

全部展开

在当前文件夹中名为 ExamplePlugin.m 的文件中创建一个插件。覆盖 reportFinalizedResult 方法以显示每个 Test 元素的状态。

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

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

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 中推出