Main Content

matlab.unittest.plugins.XMLPlugin 类

命名空间: matlab.unittest.plugins
超类: matlab.unittest.plugins.TestRunnerPlugin

将测试结果写入 XML 文件的插件

描述

matlab.unittest.plugins.XMLPlugin 类提供将测试结果写入 XML 文件的插件。

matlab.unittest.plugins.XMLPlugin 类是一个 handle 类。

创建对象

要创建 XMLPlugin 实例,请使用 producingJUnitFormat 静态方法。

方法

全部展开

示例

全部折叠

使用 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>

版本历史记录

在 R2015b 中推出