matlab.unittest.plugins.TestReportPlugin.producingDOCX
类: matlab.unittest.plugins.TestReportPlugin
命名空间: matlab.unittest.plugins
创建生成 DOCX 测试报告的插件
语法
说明
plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX
创建一个插件,该插件在临时文件夹中生成测试结果的 DOCX 报告。此语法等效于 plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX([tempname '.docx'])
。
plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX(___,
支持上述语法中的任何输入参量组合,且可使用一个或多个名称-值参量指定选项。例如,Name,Value
)plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX("LoggingLevel","detailed")
创建一个插件,该插件生成测试报告,其中包括在 "detailed"
类别及更低级别记录的诊断。
输入参数
filename
— 测试报告文件的名称
字符串标量 | 字符向量
测试报告文件的名称,指定为以 .docx
结尾的字符串标量或字符向量。该值可以是相对于当前文件夹的路径,也可以是绝对路径。
示例: "myTestReport.docx"
示例: "C:\work\myTestReport.docx"
名称-值参数
将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参量名称,Value
是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。
示例: plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX(LoggingLevel="detailed")
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: plugin = matlab.unittest.plugins.TestReportPlugin.producingDOCX("LoggingLevel","detailed")
PageOrientation
— 报告方向
"portrait"
(默认) | "landscape"
报告方向,指定为 "portrait"
或 "landscape"
。默认情况下,该插件会生成一个纵向方向的报告。
Title
— 测试报告的标题
字符串标量 | 字符向量
测试报告的标题,指定为字符串标量或字符向量。默认情况下,插件使用 "MATLAB® Test Report"
作为标题。
示例: Title="My Test Report"
IncludingCommandWindowText
— 是否包含命令行窗口的文本输出
false
或 0
(默认) | true
或 1
是否包含命令行窗口的文本输出,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,该插件不会在测试报告中包含命令行窗口的文本输出。
IncludingPassingDiagnostics
— 是否包含通过事件的诊断
false
或 0
(默认) | true
或 1
是否包含通过事件的诊断,指定为数值或逻辑值 0
(false
) 或 1
(true
)。默认情况下,插件不在测试报告中包含通过事件的诊断。
LoggingLevel
— 记录的诊断的最大详细级别
1
(默认) | 0
| 2
| 3
| 4
| matlab.automation.Verbosity
枚举对象 | 枚举的文本表示
要在测试报告中包含的已记录诊断的最大详细级别,指定为从 0
到 4
的整数标量、matlab.automation.Verbosity
枚举对象或枚举的文本表示。插件包括在指定级别及更低级别记录的诊断。
数值表示 | 枚举成员名称 | 详细程度描述 |
---|---|---|
0 | None | 无信息 |
1 | Terse | 最少的信息 |
2 | Concise | 适中信息量 |
3 | Detailed | 部分补充信息 |
4 | Verbose | 大量补充信息 |
默认情况下,插件包含在 matlab.automation.Verbosity.Terse
级别(级别 1)记录的诊断信息。要排除所记录的诊断信息,请将 LoggingLevel
指定为 matlab.automation.Verbosity.None
(级别 0)。
记录的诊断是您使用 log (TestCase)
和 log (Fixture)
方法为测试框架提供的诊断。
示例: "LoggingLevel","detailed"
示例
生成 DOCX 格式的测试报告
根据两个测试文件创建一个测试套件,运行该套件,并生成 .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)
生成包含通过诊断的 DOCX 报告
根据一个基于函数的测试创建一个测试套件,运行该套件,并生成结果报告。包含通过诊断信息和命令行窗口的文本输出。
在您的工作文件夹中创建一个名为 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.docx
的 TestReportPlugin
。在报告中包含通过诊断信息和命令行窗口的文本输出。
docxFile = 'MyTestReport2.docx'; plugin = TestReportPlugin.producingDOCX(docxFile,... '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.docx
打开测试报告。
open(docxFile)
版本历史记录
在 R2016b 中推出R2023b: 修改测试报告标题
要修改测试报告的标题,请指定 Title
名称-值参量。
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)