Plugin to Generate Custom Test Output Format - MATLAB & Simulink (original) (raw)

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 thematlab.unittest.plugins.TestRunnerPlugin class. In the plugin class:

classdef ExampleCustomPlugin < matlab.unittest.plugins.TestRunnerPlugin properties (Access=private) Stream end

methods
    function p = ExampleCustomPlugin(stream)
        if ~nargin
            stream = matlab.automation.streams.ToStandardOutput;
        end
        validateattributes(stream, ...
            {'matlab.automation.streams.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

Create Test Class

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

Add Plugin to Test Runner and Run Tests

At the command prompt, create a test suite from theExampleTest 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.automation.streams.ToFile fname = 'YPS_test_results.txt'; p = ExampleCustomPlugin(ToFile(fname));

runner.addPlugin(p) result = runner.run(suite);

View the contents of the output file.

--- NEW TEST SESSION at 15-Oct-2022 20:30:15 ---

YPS Company - Test PASSED ### - ExampleTest/testOne in 0.014881 seconds.

YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.099099 seconds.

YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.073080 seconds.

YPS Company - Test PASSED ### - ExampleTest/testFour in 0.006351 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 15-Oct-2022 20:30:15 ---

YPS Company - Test PASSED ### - ExampleTest/testOne in 0.014881 seconds.

YPS Company - Test FAILED ### - ExampleTest/testTwo in 0.099099 seconds.

YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.073080 seconds.

YPS Company - Test PASSED ### - ExampleTest/testFour in 0.006351 seconds.

--- NEW TEST SESSION at 15-Oct-2022 20:31:00 ---

YPS Company - Test SKIPPED ### - ExampleTest/testThree in 0.018080 seconds.

See Also

matlab.unittest.plugins.TestRunnerPlugin | matlab.automation.streams.OutputStream | matlab.automation.streams.ToFile | matlab.automation.streams.ToStandardOutput

Topics