Update: Now it is much more optimized in R2017a with the inclusion of FigureDiagnostic and ScreenshotDiagnostic . Check them out before going too far along this path!
Original answer
You can do this not only for failure conditions, but also for passing conditions with a combination of user diagnostics and DiagnosticValidationPlugin diagnostics. You can do this quickly using a function descriptor, but if this is what you often want to do for many of your tests, consider creating your own Diagnostic subclass:
classdef PlotDiagnostic < matlab.unittest.diagnostics.Diagnostic properties Title Actual Expected end methods function diag = PlotDiagnostic(title, actual, expected) diag.Title = title; diag.Actual = actual; diag.Expected = expected; end function diagnose(diag) diag.DiagnosticResult = sprintf('Generating plot with title "%s"', diag.Title); f = figure('Title', diag.Title); ax = axes('Parent', f); plot(ax, 1:numel(diag.Actual), diag.Actual, 'r', 1:numel(diag.Expected), diag.Expected','b'); end end end
Then you can get a test that uses this like this:
classdef FooTest < matlab.unittest.TestCase methods(Test) function testFails(testCase) actual = 1:10; expected = fliplr(actual); testCase.verifyEqual(actual, expected, PlotDiagnostic('Title1', actual, expected)); end function testPasses(testCase) actual = 1:10; expected = actual; testCase.verifyEqual(actual, expected, PlotDiagnostic('Title2', actual, expected)); end end end
Now that you have these tests, you will see them in a crash condition. However, you can also see them in the conditions of passage using the DiagnosticsValidationPlugin, which evaluates the diagnostics even in the conditions of passage, to ensure that the diagnostic code is an error (it would be too weak not to intercept the diagnostic information from a real failure, since there was an error into a diagnostic code that is not commonly used). It will look like this:
>> import matlab.unittest.*; >> runner = TestRunner.withNoPlugins; >> runner.addPlugin(matlab.unittest.plugins.DiagnosticsValidationPlugin); >> suite = TestSuite.fromClass(?FooTest); >> runner.run(suite)
Please note that with R2014a you can write your own plugin to listen to this diagnostic diagnostics instead of using DiagnosticsValidationPlugin. Indeed, in this example, we do not use this plugin with the intent to verify that the diagnosis is an error, so it would be better to write our own plugin for this specific purpose.
In addition, in R2014b, you can use the method to connect it to another watch face. If you want, you can call:
testCase.log(Verbosity.Detailed, PlotDiagnostic('Title2', actual, expected));
Then you see only graphs when you use LoggingPlugin at the "Detailed" level of detail, for example.