matlab.unittest.plugins.TestReportPlugin 类
命名空间: matlab.unittest.plugins
超类: matlab.unittest.plugins.TestRunnerPlugin, matlab.unittest.plugins.Parallelizable
生成测试报告的插件
描述
创建对象
使用以下静态方法之一创建 TestReportPlugin 实例:
要创建一个生成 DOCX 测试报告的插件,请使用
producingDOCX静态方法。要创建一个生成 HTML 测试报告的插件,请使用
producingHTML静态方法。要创建一个生成 PDF 测试报告的插件,请使用
producingPDF静态方法。
属性
测试报告的标题,以字符串标量形式返回。您可以在创建插件的过程中指定此属性的值。默认情况下,插件使用 "MATLAB® Test Report" 作为标题。
属性:
GetAccess | public |
SetAccess | immutable |
是否包含命令行窗口的文本输出,以数据类型为 logical 的 0 或 1 形式返回。您可以在创建插件的过程中指定此属性的值。默认情况下,该插件不会在测试报告中包含命令行窗口的文本输出。
属性:
GetAccess | public |
SetAccess | immutable |
是否包含通过事件的诊断,以数据类型为 logical 的 0 或 1 形式返回。您可以在创建插件的过程中指定此属性的值。默认情况下,插件不在测试报告中包含通过事件的诊断。
属性:
GetAccess | public |
SetAccess | immutable |
要在测试报告中包含的已记录诊断的最大详细级别,以 matlab.automation.Verbosity 枚举对象形式返回。您可以在创建插件的过程中指定此属性的值。默认情况下,插件包含在 matlab.automation.Verbosity.Terse 记录的诊断信息。
记录的诊断是您使用 log (TestCase) 和 log (Fixture) 方法为测试框架提供的诊断。
属性:
GetAccess | public |
SetAccess | private |
方法
matlab.unittest.plugins.TestReportPlugin.producingDOCX | 创建生成 DOCX 测试报告的插件 |
matlab.unittest.plugins.TestReportPlugin.producingHTML | 创建生成 HTML 测试报告的插件 |
matlab.unittest.plugins.TestReportPlugin.producingPDF | 创建生成 PDF 测试报告的插件 |
示例
根据两个测试文件创建一个测试套件,运行该套件,并生成 .docx 格式的结果报告。
在您的工作文件夹中创建一个名为 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.创建一个静默测试运行器,从而不在命令行窗口输出任何信息。创建一个将输出发送到文件 MyTestReport.docx 的 TestReportPlugin。
runner = TestRunner.withNoPlugins;
docxFile = 'MyTestReport.docx';
plugin = TestReportPlugin.producingDOCX(docxFile);将该插接添加到 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\MyTestReport.docx
result =
1×284 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
282 Passed, 2 Failed, 2 Incomplete.
0.73477 seconds testing time.打开测试报告。
open(docxFile)
根据一个基于函数的测试创建一个测试套件,运行该套件,并生成结果报告。包含通过诊断信息和命令行窗口的文本输出。
在您的工作文件夹中创建一个名为 FunctionBasedTest.m 的新文件,其中包含以下基于函数的测试。测试文件包括两个失败的测试。
%% Main function to generate tests function tests = FunctionBasedTest tests = functiontests(localfunctions); end %% Test Functions function passingTest(testCase) actSolution = 13*3+7*5; expSolution = 74; verifyEqual(testCase,actSolution,expSolution) end function failingTest(testCase) actSolution = single(1); verifyTrue(testCase,actSolution) end function anotherPassingTest(testCase) verifyClass(testCase,string('some text'),'string') end function anotherFailingTest(testCase) verifyTrue(testCase,strcmp('42','everything')) end
在命令提示符下,根据 FunctionBasedTest.m 创建一个测试套件。使用默认插件创建一个将输出显示到命令行窗口中的测试运行器。
import matlab.unittest.TestRunner; import matlab.unittest.TestSuite; import matlab.unittest.plugins.TestReportPlugin; suite = testsuite('FunctionBasedTest'); runner = TestRunner.withTextOutput;
创建一个将输出发送到文件 MyTestReport2.pdf 的 TestReportPlugin。在报告中包含通过诊断信息和命令行窗口的文本输出。
pdfFile = 'MyTestReport2.pdf'; plugin = TestReportPlugin.producingPDF(pdfFile,... 'IncludingPassingDiagnostics',true,'IncludingCommandWindowText',true);
将该插接添加到 TestRunner 并运行套件。
runner.addPlugin(plugin); result = runner.run(suite);
Running FunctionBasedTest
.
================================================================================
Verification failed in FunctionBasedTest/failingTest.
---------------------
Framework Diagnostic:
---------------------
verifyTrue failed.
--> The value must be logical. It is of type "single".
Actual single:
1
------------------
Stack Information:
------------------
In C:\Work\FunctionBasedTest.m (failingTest) at 15
================================================================================
..
================================================================================
Verification failed in FunctionBasedTest/anotherFailingTest.
---------------------
Framework Diagnostic:
---------------------
verifyTrue failed.
--> The value must evaluate to "true".
Actual logical:
0
------------------
Stack Information:
------------------
In C:\Work\FunctionBasedTest.m (anotherFailingTest) at 23
================================================================================
.
Done FunctionBasedTest
__________
Failure Summary:
Name Failed Incomplete Reason(s)
===================================================================================
FunctionBasedTest/failingTest X Failed by verification.
-----------------------------------------------------------------------------------
FunctionBasedTest/anotherFailingTest X Failed by verification.
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\MyTestReport2.pdf打开测试报告。
open(pdfFile)
在您当前工作文件夹下的文件中,创建 FigurePropTest 测试类。如果 failingTest 测试方法失败(在此示例中总是如此),它会使用 FigureDiagnostic 保存图窗,以便于您以后检查。
classdef FigurePropTest < matlab.unittest.TestCase properties TestFigure end methods (TestMethodSetup) function createFigure(testCase) testCase.TestFigure = figure; end end methods (TestMethodTeardown) function closeFigure(testCase) close(testCase.TestFigure) end end methods (Test) function defaultCurrentPoint(testCase) cp = testCase.TestFigure.CurrentPoint; testCase.verifyEqual(cp,[0 0], ... 'Default current point is incorrect') end function defaultCurrentObject(testCase) import matlab.unittest.constraints.IsEmpty co = testCase.TestFigure.CurrentObject; testCase.verifyThat(co,IsEmpty, ... 'Default current object should be empty') end function failingTest(testCase) import matlab.unittest.diagnostics.FigureDiagnostic fig = testCase.TestFigure; ax = axes(fig); surf(ax,peaks) testCase.verifyEmpty(testCase.TestFigure.Children, ... FigureDiagnostic(testCase.TestFigure)) end end end
在命令提示符下,创建一个测试套件。
suite = testsuite('FigurePropTest');创建一个静默测试运行器,以记录诊断信息并生成 PDF 报告。
import matlab.unittest.plugins.DiagnosticsRecordingPlugin import matlab.unittest.plugins.TestReportPlugin runner = matlab.unittest.TestRunner.withNoPlugins; runner.addPlugin(DiagnosticsRecordingPlugin) runner.addPlugin(TestReportPlugin.producingPDF('MyTestReport.pdf'))
将默认工件根文件夹更改为您的当前工作文件夹。
runner.ArtifactsRootFolder = pwd;
运行测试。第三个测试失败。
results = runner.run(suite)
Generating test report. Please wait.
Preparing content for the test report.
Adding content to the test report.
Writing test report to file.
Test report has been saved to:
C:\wok\MyTestReport.pdf
results =
1×3 TestResult array with properties:
Name
Passed
Failed
Incomplete
Duration
Details
Totals:
2 Passed, 1 Failed (rerun), 0 Incomplete.
8.3355 seconds testing time.显示第三个测试的测试诊断结果。测试框架保存了与第三个测试相关的两个工件。默认情况下,FigureDiagnostic 对象将图窗同时保存为 PNG 文件和 FIG 文件。
results(3).Details.DiagnosticRecord.TestDiagnosticResults
ans =
DiagnosticResult with properties:
Artifacts: [1×2 matlab.automation.diagnostics.FileArtifact]
DiagnosticText: 'Figure saved to:↵--> C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig↵--> C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.png'
显示第一个工件的存储位置。
results(3).Details.DiagnosticRecord.TestDiagnosticResults.Artifacts(1)
ans =
FileArtifact with properties:
Name: "Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig"
Location: "C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb"
FullPath: "C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig"
要检查与失败测试相关的图像,请打开位于 FullPath 字段所示位置的文件。此外,由于您生成了 PDF 测试报告,因此 MyTestReport.pdf 中也会包含捕获的图像。测试报告还包含工件路径。
版本历史记录
在 R2016b 中推出为了支持修改测试报告标题,TestReportPlugin 类提供一个名为 Title 的新属性。
使用 TestReportPlugin 类生成的测试报告显示带标记的测试套件元素的测试标记。您可以生成 DOCX、HTML 和 PDF 格式的带标记的测试报告。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)