Main Content

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

matlab.unittest.plugins.TestReportPlugin.producingHTML

类: matlab.unittest.plugins.TestReportPlugin
包: matlab.unittest.plugins

构造生成 .html 报告的插件

说明

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

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

示例

matlab.unittest.plugins.TestReportPlugin.producingHTML(htmlFolder) 将报告保存到 htmlFolder 文件夹中。

matlab.unittest.plugins.TestReportPlugin.producingHTML(___,Name,Value) 构造一个插件,并通过一个或多个 Name,Value 对组参数指定其他选项。您可以将此语法与前面语法中的任何参数结合使用。

输入参数

全部展开

输出文件夹,指定为字符向量或字符串标量。htmlFolder 可以是相对路径或绝对路径。默认情况下,在该文件夹中,报告的主文件为 index.html。要更改主文件的名称,请使用 'MainFile' 名称-值对组参数。

示例: 'TestRunOutput'

示例: 'C:\myWork\testResults'

名称-值参数

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

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

示例: TestReportPlugin.producingHTML('myTestOutput','MainFile','main.html') 创建一个插件,它将结果输出到 myTestOutput 文件夹,而且主文件名为 main.html,而不是 index.html

HTML 主文件的名称,指定为字符向量或字符串标量。

示例: 'main.html'

在报告中包含命令行窗口文本,指定为 falsetrue。默认情况下,IncludingCommandWindowTextfalse,即不在报告中包含命令行窗口的文本输出。要在报告中包含命令行窗口文本,请将 IncludingCommandWindowText 指定为 true

数据类型: logical

包含通过事件诊断信息,指定为 falsetrue。默认情况下,IncludingPassingDiagnosticsfalse,即不在报告中包含通过事件的诊断信息。要在报告中包含通过事件的诊断信息,请将 IncludingPassingDiagnostics 指定为 true

数据类型: logical

插件实例包含的所记录诊断信息的最高级别,指定为介于 0 和 4 之间的整数值、matlab.unittest.Verbosity 枚举对象,或对应于预定义枚举成员名称之一的字符串标量或字符向量。该插件包含在此级别和更低级别记录的诊断信息。整数值对应于 matlab.unittest.Verbosity 枚举的成员。

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

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

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

记录的诊断信息是您通过调用 log (TestCase)log (Fixture) 方法提供给测试框架的诊断信息。

示例

全部展开

根据两个测试文件创建一个测试套件,运行该套件,并生成 .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 中推出