Main Content

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

matlab.unittest.plugins.TestReportPlugin.producingHTML

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

创建生成 HTML 测试报告的插件

说明

plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML 创建一个插件,该插件在临时文件夹中生成测试结果的 HTML 报告。在该文件夹中,报告的主文件为 index.html。如果您使用此插件重新运行测试套件,则 MATLAB® 会覆盖文件夹中的内容。

此语法等效于 plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML(tempname)

示例

plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML(folderName) 将报告保存到指定的文件夹。

plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML(___,Name,Value) 支持上述语法中的任何输入参量组合,且可使用一个或多个名称-值参量指定选项。例如,plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML("MainFile","main.html") 创建生成测试报告的插件,报告的主文件名为 main.html,而不是 index.html

输入参数

全部展开

测试报告文件夹的名称,指定为字符串标量或字符向量。如果您指定相对路径,则该路径必须在当前文件夹中。否则,您必须指定完整路径。

示例: "myTestReport"

示例: "C:\work\myTestReport"

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

示例: plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML(MainFile="main.html")

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

示例: plugin = matlab.unittest.plugins.TestReportPlugin.producingHTML("MainFile","main.html")

主 HTML 文件的名称,指定为以 .html.htm 结尾的字符串标量或字符向量。如果未指定,插件会将报告的主文件命名为 index.html

示例: MainFile="main.html"

测试报告的标题,指定为字符串标量或字符向量。默认情况下,插件使用 "MATLAB Test Report" 作为标题。

示例: Title="My Test Report"

是否包含命令行窗口的文本输出,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,该插件不会在测试报告中包含命令行窗口的文本输出。

是否包含通过事件的诊断,指定为数值或逻辑值 0 (false) 或 1 (true)。默认情况下,插件不在测试报告中包含通过事件的诊断。

要在测试报告中包含的已记录诊断的最大详细级别,指定为从 04 的整数标量、matlab.automation.Verbosity 枚举对象或枚举的文本表示。插件包括在指定级别及更低级别记录的诊断。

数值表示枚举成员名称详细程度描述
0None

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

默认情况下,插件包含在 matlab.automation.Verbosity.Terse 级别(级别 1)记录的诊断信息。要排除所记录的诊断信息,请将 LoggingLevel 指定为 matlab.automation.Verbosity.None(级别 0)。

记录的诊断是您使用 log (TestCase)log (Fixture) 方法为测试框架提供的诊断。

示例: "LoggingLevel","detailed"

示例

全部展开

根据两个测试文件创建一个测试套件,运行该套件,并生成 HTML 报告的结果报告。

在您的工作文件夹中创建一个名为 ScriptBasedTest.m 的新文件,其中包含以下测试脚本。该脚本包括两个失败且未完成的测试。

%% Test double class
expSolution = 'double';
actSolution = ones;
assert(isa(actSolution,expSolution))

%% Test single class
expSolution = 'single';
actSolution = ones('single');
assert(isa(actSolution,expSolution))

%% Test uint16 class
expSolution = 'uint16';
actSolution = ones('uint16');
assert(isa(actSolution,expSolution))

%% Test that fails
assert(false==true);

%% Another test that fails
assert(strcmp('correlation','causation'))

创建一个名为 ClassBasedTest.m 的文件,其中包含以下测试类。该类包含一个失败的测试,经过参数化后,该测试产生九个失败的测试。

classdef ClassBasedTest < matlab.unittest.TestCase
    properties (ClassSetupParameter)
        generator = {'twister','combRecursive','multFibonacci'};
    end
    properties (MethodSetupParameter)
        seed = {0,123,4294967295};
    end
    properties (TestParameter)
        dim1 = struct('small',1,'medium',2,'large',3);
        dim2 = struct('small',2,'medium',3,'large',4);
        dim3 = struct('small',3,'medium',4,'large',5);
        type = {'single','double'};
    end
    methods (TestClassSetup)
        function ClassSetup(testCase,generator)
            orig = rng;
            testCase.addTeardown(@rng,orig)
            rng(0, generator)
        end
    end
    methods (TestMethodSetup)
        function MethodSetup(testCase,seed)
            orig = rng;
            testCase.addTeardown(@rng,orig)
            rng(seed)
        end
    end
    methods (Test, ParameterCombination='sequential')
        function testSize(testCase,dim1,dim2,dim3)
            testCase.verifySize(rand(dim1,dim2,dim3),[dim1 dim2 dim3])
        end 
    end
    methods (Test, ParameterCombination='pairwise')
        function testRepeatable(testCase,dim1,dim2,dim3)
            state = rng;
            firstRun = rand(dim1,dim2,dim3);
            rng(state)
            secondRun = rand(dim1,dim2,dim3);
            testCase.verifyEqual(firstRun,secondRun);
        end
    end
    methods (Test)
        function testClass(testCase,dim1,dim2,type)
            testCase.verifyClass(rand(dim1,dim2,type),type)
        end
    end
end

在命令提示符下,根据两个测试文件创建一个测试套件。

import matlab.unittest.TestRunner;
import matlab.unittest.TestSuite;
import matlab.unittest.plugins.TestReportPlugin;

suite = testsuite({'ScriptBasedTest','ClassBasedTest'})
suite = 

  1×284 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   17 Unique Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

创建一个静默测试运行器,从而不在命令行窗口输出任何信息。创建 TestReportPlugin,它在名为 myResults 的文件夹中生成 .html 格式的测试报告。

runner = TestRunner.withNoPlugins;
htmlFolder = 'myResults';
plugin = TestReportPlugin.producingHTML(htmlFolder);

将该插接添加到 TestRunner 并运行套件。

runner.addPlugin(plugin);
result = runner.run(suite)
Generating report. Please wait.
    Preparing content for the report.
    Adding content to the report.
    Writing report to file.
Report has been saved to: C:\work\myResults\index.html

result = 

  1×284 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   282 Passed, 2 Failed, 2 Incomplete.
   1.6712 seconds testing time.

通过点击保存的文件的名称,打开测试报告。在此示例中,文件名为 C:\work\myResults\index.html

版本历史记录

在 R2017b 中推出

全部展开