matlab.unittest.TestRunner.addPlugin - Add plugin to test runner - MATLAB (original) (raw)

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

Add plugin to test runner

Syntax

Description

addPlugin([runner](#mw%5F24ebea04-84d8-4982-9cb7-1e4d6f80d4e9),[plugin](#mw%5Ff108c64b-3e6f-4ead-b5e9-8d4ed4619ff8)) adds the specified plugin to the test runner.

example

Input Arguments

expand all

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

Examples

expand all

Generate JUnit-style test results by adding anXMLPlugin 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.

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