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

3 views (last 30 days)
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?

Accepted Answer

Andy Campbell
Andy Campbell on 6 Jan 2015
Edited: Andy Campbell on 13 Jan 2015
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:
  2 Comments
Andy Campbell
Andy Campbell on 22 Apr 2016
Note that now with the DiagnosticsRecordingPlugin you can get programmatic access to these diagnostics without writing your own plugin. Note that runtests includes this plugin out of the box so you likely don't even need much configuration to access this data.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!