MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Create Custom Plugin


This example shows how to create a custom plugin that counts the number of passing and
failing assertions when running a specified test suite. The plugin prints a brief summary
at the end of the testing.

Create AssertionCountingPlugin Class

In a file in your working folder, create a new class, AssertionCountingPlugin, that
inherits from the matlab.unittest.plugins.TestRunnerPlugin class. For a
complete version of the code for an AssertionCountingPlugin, see
"AssertionCountingPlugin Class Definition Summary".

Keep track of the number of passing and failing assertions. Within a properties block,
create NumPassingAssertions and NumFailingAssertions properties to pass the
data between methods.

properties
NumPassingAssertions = 0;
NumFailingAssertions = 0;
end

Extend Running of TestSuite

Implement the runTestSuite method in a methods block with protected access.

methods (Access = protected)
function runTestSuite(plugin, pluginData)
suiteSize = numel(pluginData.TestSuite);
fprintf('## Running a total of %d tests\n', suiteSize)

plugin.NumPassingAssertions = 0;
plugin.NumFailingAssertions = 0;

[email protected](...
plugin, pluginData);

fprintf('## Done running tests\n')
plugin.printAssertionSummary()
end
end

The test framework evaluates this method one time. It displays information about the
total number of tests, initializes the assertion count, and invokes the superclass method.

Create Custom Plugin
Free download pdf