Info

This question is closed. Reopen it to edit or answer.

How to create a plugin that stores the exception from a failing test

1 view (last 30 days)
Daniel Wallis
Daniel Wallis on 4 Jul 2014
Closed: MATLAB Answer Bot on 20 Aug 2021
Hi,
I am running MATLAB R2014a and I'm running single tests with the command:
result = run(NameOfTestClass, 'NameOfTestMethod')
I can then find out if the test passed or failed by looking at the value of:
result.Passed
and
result.Failed
Is it possible that I can store in the result the reason that the test failed, and the stacktrace?
Would this be via writing a plugin?
Thanks

Answers (3)

Andy Campbell
Andy Campbell on 7 Jul 2014
Edited: Andy Campbell on 7 Jul 2014
Hi Daniel,
There is currently no plugin included with the framework that directly stores this information on its properties. However, this information is accessible if you write your own plugin. What you need to do when writing your plugin is:
  1. Implement the createTestMethodInstance and/or createTestClassInstance method on the plugin in order to get a hold of the TestCase instances passed to the test
  2. Add listeners to these instances to listen to the possible failure events
  3. When the events are triggered, the listeners are either passed a QualificationEventData or an ExceptionEventData which contains this information and can be stored away and referenced later.
Also, it sounds like you really want programmatic access to this information, but just so you are aware you can leverage a different OutputStream to send the textual output from something like the FailureDiagnosticsPlugin. That is you can:
import matlab.unittest.TestSuite;
import matlab.unittest.TestRunner;
import matlab.unittest.plugins.ToFile;
import matlab.unittest.plugins.FailureDiagnosticsPlugin;
suite = TestSuite.fromClass(?NameOfClass);
plugin = FailureDiagnosticsPlugin(ToFile(some_file_on_disk));
runner = TestRunner.withNoPlugins;
runner.addPlugin(plugin);
result = runner.run(suite);
You can also create your own OutputStream such as a stream which just holds all of the output in a property of a class, but it will all be textual information, whereas if you use the listener approach you will be able to access the data as MATLAB objects and data structures.
Hope that helps!
Andy

per isakson
per isakson on 6 Jul 2014
Edited: per isakson on 6 Jul 2014
  2 Comments
per isakson
per isakson on 8 Jul 2014
I still run R2013a and I use
msg_str = evalc('results = run( test_runner, test_suite );');
Andy's words should be final!

Andy Campbell
Andy Campbell on 24 Jan 2017
This is probably a dup of this question as per isakson points out. Be sure to check there to see some more up to date answers that incorporate new features of the framework, like the DiagnosticsRecordingPlugin.

Community Treasure Hunt

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

Start Hunting!