Main Content

matlab.unittest.plugins.XMLPlugin Class

Namespace: matlab.unittest.plugins
Superclasses: matlab.unittest.plugins.TestRunnerPlugin

Plugin that writes test results to XML file

Description

The matlab.unittest.plugins.XMLPlugin class provides a plugin that writes test results to an XML file.

The matlab.unittest.plugins.XMLPlugin class is a handle class.

Creation

To create an XMLPlugin instance, use the producingJUnitFormat static method.

Methods

expand all

Examples

collapse all

Produce test results in JUnit-style XML format by using the XMLPlugin class.

In your current folder, create a function-based test file named sampleTest.m. The file contains two tests that pass and one test that fails.

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

Import the XMLPlugin class.

import matlab.unittest.plugins.XMLPlugin

Create a test runner with a plugin that produces test results in JUnit-style XML format. To create the plugin, use the producingJUnitFormat static method.

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

Create a test suite from the test file and run the tests. The test runner runs the tests, and the plugin saves the test results to a file named results.xml in your current folder.

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

View the contents of the generated test artifact. The results in the file indicate that testA and testC passed but testB failed due to a verification failure.

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>

Version History

Introduced in R2015b