Main Content

matlab.unittest.plugins.XMLPlugin.producingJUnitFormat

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

创建以 JUnit 样式 XML 格式生成测试结果的插件

说明

示例

plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat(filename) 创建一个插件,该插件以 JUnit 样式 XML 格式将测试结果写入指定的文件。

使用此插件,您可以将 MATLAB® 单元测试结果集成到可识别 JUnit 样式 XML 格式的第三方系统中。例如,您可将测试结果与持续集成平台(如 Jenkins®、TeamCity® 或 Azure®)开发运营一体化。

plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat(filename,"OutputDetail",level) 创建一个插件,其中包含指定级别的失败事件的详细信息。

输入参数

全部展开

存储测试结果的文件的名称,指定为以 .xml 结尾的字符串标量或字符向量。该值可以是相对于当前文件夹的路径,也可以是绝对路径。如果该文件存在,插件会覆盖其内容。

示例: "results.xml"

示例: "C:\work\results.xml"

要包含的失败事件详细信息的级别,指定为从 04 的整数标量、matlab.automation.Verbosity 枚举对象或枚举的文本表示。默认情况下,该插件包括 matlab.automation.Verbosity.Detailed 级别(级别 3)的失败事件的详细信息。

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

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

示例: plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat("myResults.xml","OutputDetail","verbose") 创建一个插件,其中包括 matlab.automation.Verbosity.Verbose 级别的失败事件的详细信息。

属性

Statictrue

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

示例

全部展开

使用 XMLPlugin 类以 JUnit 样式 XML 格式生成测试结果。

在当前文件夹中,创建一个名为 sampleTest.m 的基于函数的测试文件。该文件包含两个能够通过的测试和一个失败的测试。

function tests = sampleTest
tests = functiontests(localfunctions);
end

function testA(testCase)      % Test passes
verifyEqual(testCase,2+3,5)
end

function testB(testCase)      % Test fails
verifyGreaterThan(testCase,13,42)
end

function testC(testCase)      % Test passes
verifySubstring(testCase,"Hello World!","llo")
end

导入 XMLPlugin 类。

import matlab.unittest.plugins.XMLPlugin

用插件创建一个测试运行器,该插件以 JUnit 样式 XML 格式生成测试结果。要创建插件,请使用 producingJUnitFormat 静态方法。

runner = testrunner("minimal");
filename = "results.xml";
plugin = XMLPlugin.producingJUnitFormat(filename);
addPlugin(runner,plugin)

从测试文件创建一个测试套件并运行测试。测试运行器运行测试,插件将测试结果保存到当前文件夹中名为 results.xml 的文件中。

suite = testsuite("sampleTest.m");
run(runner,suite);

查看生成的测试工件的内容。文件中的结果表明 testAtestC 通过,但 testB 由于验证失败而未通过。

disp(fileread(filename))
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<testsuites>
  <testsuite errors="0" failures="1" name="sampleTest" skipped="0" tests="3" time="1.3417">
    <testcase classname="sampleTest" name="testA" time="0.38058"/>
    <testcase classname="sampleTest" name="testB" time="0.92769">
      <failure type="VerificationFailure">Verification failed in sampleTest/testB.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyGreaterThan failed.
    --&gt; The value must be greater than the minimum value.
    
    Actual Value:
        13
    Minimum Value (Exclusive):
        42
    ------------------
    Stack Information:
    ------------------
    In C:\work\sampleTest.m (testB) at 10</failure>
    </testcase>
    <testcase classname="sampleTest" name="testC" time="0.033431"/>
  </testsuite>
</testsuites>

提示

  • 对于基于脚本或基于函数的测试,结果 XML 文件中 <testcase> 元素的 classname 属性的值是测试文件的名称。

版本历史记录

在 R2015b 中推出