This example shows how to create a plugin that uses a custom format to write finalized test results to an output stream.
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@... matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData); end function reportFinalizedResult(plugin,pluginData) thisResult = pluginData.TestResult; if thisResult.Passed status = 'PASSED'; elseif thisResult.Failed status = 'FAILED'; elseif thisResult.Incomplete status = 'SKIPPED'; end plugin.Stream.print(... '### YPS Company - Test %s ### - %s in %f seconds.\n',... status,thisResult.Name,thisResult.Duration) reportFinalizedResult@... matlab.unittest.plugins.TestRunnerPlugin(plugin,pluginData) end end end
In your working folder, create the file ExampleTest.m
containing the following test class. In this test class, two of the tests pass and the others result in a verification or assumption failure.
classdef ExampleTest < matlab.unittest.TestCase methods(Test) function testOne(testCase) testCase.assertGreaterThan(5,1) end function testTwo(testCase) wrongAnswer = 'wrong'; testCase.verifyEmpty(wrongAnswer,'Not Empty') testCase.verifyClass(wrongAnswer,'double','Not double') end function testThree(testCase) testCase.assumeEqual(7*2,13,'Values not equal') end function testFour(testCase) testCase.verifyEqual(3+2,5); end end end
At the command prompt, create a test suite from the ExampleTest
class, and create a test runner.
import matlab.unittest.TestSuite import matlab.unittest.TestRunner suite = TestSuite.fromClass(?ExampleTest); runner = TestRunner.withNoPlugins;
Create an instance of ExampleCustomPlugin
and add it to the test runner. Run the tests.
import matlab.unittest.plugins.ToFile fname = 'YPS_test_results.txt'; p = ExampleCustomPlugin(ToFile(fname)); runner.addPlugin(p) result = runner.run(suite);
View the contents of the output file.
type(fname)
--- NEW TEST SESSION at 26-Jan-2015 10:41:24 --- ### YPS Company - Test PASSED ### - ExampleTest/testOne in 0.123284 seconds. ### YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.090363 seconds. ### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.518044 seconds. ### YPS Company - Test PASSED ### - ExampleTest/testFour in 0.020599 seconds.
Rerun the Incomplete
tests using the same test runner. View the contents of the output file.
suiteFiltered = suite([result.Incomplete]); result2 = runner.run(suiteFiltered); type(fname)
--- NEW TEST SESSION at 26-Jan-2015 10:41:24 --- ### YPS Company - Test PASSED ### - ExampleTest/testOne in 0.123284 seconds. ### YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.090363 seconds. ### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.518044 seconds. ### YPS Company - Test PASSED ### - ExampleTest/testFour in 0.020599 seconds. --- NEW TEST SESSION at 26-Jan-2015 10:41:58 --- ### YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.007892 seconds.
matlab.unittest.plugins.OutputStream
| matlab.unittest.plugins.TestRunnerPlugin
| ToFile
| ToStandardOutput