Main Content

addPlugin

Class: matlab.unittest.TestRunner
Namespace: matlab.unittest

Add plugin to test runner

Description

addPlugin(runner,plugin) adds the specified plugin to the test runner.

example

Input Arguments

expand all

Test runner, specified as a matlab.unittest.TestRunner object.

Plugin, specified as a matlab.unittest.plugins.TestRunnerPlugin object.

Examples

expand all

Generate JUnit-style test results by adding an XMLPlugin instance to the test runner.

In a file named eyeTest.m in your current folder, create a function-based test to test the eye function.

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

function doubleClassTest(testCase)
actual = eye;
verifyClass(testCase,actual,"double")
end

function singleClassTest(testCase)
actual = eye("single");
verifyClass(testCase,actual,"single")
end

function uint16ClassTest(testCase)
actual = eye("uint16");
verifyClass(testCase,actual,"uint16")
end

function sizeTest(testCase)
expected = [7 13];
actual = eye(expected);
verifySize(testCase,actual,expected)
end

function valueTest(testCase)
actual = eye(42);
verifyEqual(testCase,unique(diag(actual)),1)    % Diagonal values must be 1
verifyEqual(testCase,unique(triu(actual,1)),0)  % Upper triangular values must be 0
verifyEqual(testCase,unique(tril(actual,-1)),0) % Lower triangular values must be 0
end

To run the tests, first import the classes used in this example.

import matlab.unittest.TestRunner
import matlab.unittest.plugins.XMLPlugin

Create a test suite from the tests in eyeTest.m.

suite = testsuite("eyeTest.m");

Create a test runner with no plugins. This code creates a silent runner that produces no output.

runner = matlab.unittest.TestRunner.withNoPlugins;

You can now add any plugins you choose. Create a plugin that writes JUnit-style XML output to the file myTestResults.xml in your current folder. Then, add the plugin to the test runner.

xmlFile = "myTestResults.xml";
p = XMLPlugin.producingJUnitFormat(xmlFile);
addPlugin(runner,p)

Run the tests. In this example, all the tests pass.

results = run(runner,suite);

View the contents of the generated artifact.

disp(fileread(xmlFile))
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<testsuites>
  <testsuite errors="0" failures="0" name="eyeTest" skipped="0" tests="5" time="0.25082">
    <testcase classname="eyeTest" name="doubleClassTest" time="0.015066"/>
    <testcase classname="eyeTest" name="singleClassTest" time="0.0042728"/>
    <testcase classname="eyeTest" name="uint16ClassTest" time="0.0046594"/>
    <testcase classname="eyeTest" name="sizeTest" time="0.013599"/>
    <testcase classname="eyeTest" name="valueTest" time="0.21322"/>
  </testsuite>
</testsuites>

Run tests using a plugin that directs the names of tests being run to an output stream. Pass a ToStandardOutput instance to the plugin so that it directs the text it produces to the screen.

Create Custom Plugin

In a file named ExamplePlugin.m in your current folder, create the ExamplePlugin class, which overrides the runTest method of TestRunnerPlugin. The plugin directs the name of each test being run to the output stream specified during construction of the plugin.

classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin
    properties (SetAccess=immutable)
        Output
    end

    methods
        function plugin = ExamplePlugin(stream)
            arguments
                stream (1,1) matlab.automation.streams.OutputStream
            end
            plugin.Output = stream;
        end
    end

    methods (Access=protected)
        function runTest(plugin,pluginData)
            print(plugin.Output,"### Running test: %s\n",pluginData.Name)
            % Invoke the superclass method
            runTest@matlab.unittest.plugins.TestRunnerPlugin( ...
                plugin,pluginData)
        end
    end
end

Create Example Test Class

In a file named ZerosTest.m in your current folder, create the ZerosTest class, which tests the zeros function.

classdef ZerosTest < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        size = struct("s2d",[3 3],"s3d",[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase,size,type)
            testCase.verifyClass(zeros(size,type),type)
        end
        
        function testSize(testCase,size)
            testCase.verifySize(zeros(size),size)
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros,"double")
        end

        function testDefaultSize(testCase)
            testCase.verifySize(zeros,[1 1])
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0)
        end
    end
end

Add Plugin to Test Runner and Run Tests

To run the tests, first import the classes used in this example.

import matlab.unittest.TestRunner
import matlab.automation.streams.ToStandardOutput

Create a test suite from the ZerosTest class.

suite = testsuite("ZerosTest");

Create a test runner with no plugins. This code creates a silent runner that produces no output.

runner = testrunner("minimal");

You can now add any plugins you choose. Create an ExamplePlugin instance that directs text output to the screen.

plugin = ExamplePlugin(ToStandardOutput);

Add the plugin to the test runner and run the tests. As the tests run, the names of the tests appear on the screen.

runner.addPlugin(plugin)
results = runner.run(suite);
### Running test: ZerosTest/testClass(size=s2d,type=single)
### Running test: ZerosTest/testClass(size=s2d,type=double)
### Running test: ZerosTest/testClass(size=s2d,type=uint16)
### Running test: ZerosTest/testClass(size=s3d,type=single)
### Running test: ZerosTest/testClass(size=s3d,type=double)
### Running test: ZerosTest/testClass(size=s3d,type=uint16)
### Running test: ZerosTest/testSize(size=s2d)
### Running test: ZerosTest/testSize(size=s3d)
### Running test: ZerosTest/testDefaultClass
### Running test: ZerosTest/testDefaultSize
### Running test: ZerosTest/testDefaultValue

Version History

Introduced in R2013a