Plugin to Generate Custom Test Output Format
This example shows how to create a plugin that uses a custom format to write finalized
test results to an output stream.
Create Plugin
In a file in your working folder, create a class, ExampleCustomPlugin, that inherits from
the matlab.unittest.plugins.TestRunnerPlugin class. In the plugin class:
- Define a Stream property on the plugin that stores the OutputStream instance. By
default, the plugin writes to standard output. - Override the default runTestSuite method of TestRunnerPlugin to output text
that indicates the test runner is running a new test session. This information is
especially useful if you are writing to a single log file, as it allows you to differentiate
the test runs. - Override the default reportFinalizedResult method of TestRunnerPlugin to
write finalized test results to the output stream. You can modify the print method to
output the test results in a format that works for your test logs or continuous
integration system.
classdef ExampleCustomPlugin < matlab.unittest.plugins.TestRunnerPlugin
properties (Access=private)
Stream
end
methods
function p = ExampleCustomPlugin(stream)
if ~nargin
stream = matlab.unittest.plugins.ToStandardOutput;
end
validateattributes(stream,...
{'matlab.unittest.plugins.OutputStream'},{})
p.Stream = stream;
end
end
methods (Access=protected)
function runTestSuite(plugin,pluginData)
plugin.Stream.print('\n--- NEW TEST SESSION at %s ---\n',...
char(datetime))
runTestSuite@...
33 Unit Testing