Main Content

matlab.unittest.plugins.XMLPlugin.producingJUnitFormat

Class: matlab.unittest.plugins.XMLPlugin
Namespace: matlab.unittest.plugins

Create plugin that produces test results in JUnit-style XML format

Description

plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat(filename) creates a plugin that writes test results in JUnit-style XML format to the specified file.

Using this plugin, you can integrate MATLAB® unit test results into third-party systems that recognize the JUnit-style XML format. For example, you can integrate test results with continuous integration platforms, such as Jenkins®, TeamCity®, or Azure® DevOps.

example

plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat(filename,"OutputDetail",level) creates a plugin that includes failing event details at the specified level.

Input Arguments

expand all

Name of the file to store the test results, specified as a string scalar or character vector ending in .xml. The value can be a path relative to the current folder or an absolute path. If the file exists, the plugin overwrites its contents.

Example: "results.xml"

Example: "C:\work\results.xml"

Level of failing event details to include, specified as an integer scalar from 0 through 4, a matlab.automation.Verbosity enumeration object, or a text representation of the enumeration. By default, the plugin includes failing event details at the matlab.automation.Verbosity.Detailed level (level 3).

Numeric RepresentationEnumeration Member NameVerbosity Description
0None

No information

1Terse

Minimal information

2Concise

Moderate amount of information

3Detailed

Some supplemental information

4Verbose

Lots of supplemental information

Example: plugin = matlab.unittest.plugins.XMLPlugin.producingJUnitFormat("myResults.xml","OutputDetail","verbose") creates a plugin that includes failing event details at the matlab.automation.Verbosity.Verbose level.

Attributes

Statictrue

To learn about attributes of methods, see Method Attributes.

Examples

expand 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>

Tips

  • For script-based or function-based tests, the value of the classname attribute of the <testcase> element in the resulting XML file is the name of your test file.

Version History

Introduced in R2015b