MATLAB: How to assign the Test Diagnostic provided by the Unit Testing framework in the Command Window to a variable

test diagnosticunittest

While runnig tests with the Unit Testing framework the defined Test Diagnostic is displayed in the Command Window: ================================================================================ Verification failed in mbd_ZaehlerUnitTest/testTestCase01(UeberlaufAbfangen=off,InternerDatentyp=uint8,Abtastzeit=T_SAMP,ModelCoverage=No).
----------------
Test Diagnostic:
----------------
xxx
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
In order to create an overall test report I need to assign this diagnostic text to a string variable. How can I Do this?

Best Answer

Hi Malek,
You can get programmatic access to this information by creating and installing a TestRunnerPlugin for the test run. This plugin can then listen to qualification failed events (like verification failures, assertion failure, etc) and this diagnostic information is contained in the TestDiagnosticResult property of the event data as a cell array of strings.
Here is a quick example that save all of the test diagnostics for failing verifications in test methods. Note this could expand to cover other cases like assertions, assumptions, etc and other scopes like TestClassSetup, but this might help get you started.
classdef TestDiagnosticSaverPlugin < matlab.unittest.plugins.TestRunnerPlugin
properties
TestDiagnosticData
end
methods (Access = protected)
function runTestSuite(plugin, pluginData)
plugin.TestDiagnosticData = []; % Reinitialize for each test run
runTestSuite@...
matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
end
function testCase = createTestMethodInstance(plugin, pluginData)
testCase = createTestMethodInstance@...
matlab.unittest.plugins.TestRunnerPlugin(plugin, pluginData);
testName = pluginData.Name;
testCase.addlistener('VerificationFailed', ...
@(~,event)plugin.storeTestDiagnosticsData(event,pluginData.Name));
end
end
methods (Access = private)
function storeTestDiagnosticsData(plugin,eventData,name,failureType)
s.Name = {name};
s.TestDiagnostics = eventData.TestDiagnosticResult;
plugin.TestDiagnosticData = [plugin.TestDiagnosticData; struct2table(s)];
end
end
end
With the above plugin you could then install it on a runner:
>> import matlab.unittest.TestRunner;
>> import matlab.unittest.TestSuite;
>> suite = TestSuite.fromFile('mbd_ZaehlerUnitTest.m')
>> runner = TestRunner.withTextOutput;
>> plugin = TestDiagnosticSaverPlugin;
>> runner.addPlugin(plugin);
>> runner.run(suite)
>> plugin.TestDiagnosticData
Hope that helps! Here are a few links you might find interesting to help you do this: