matlab.unittest.plugins.OutputStream 类
包: matlab.unittest.plugins
决定文本输出发送位置的接口
说明
OutputStream
接口是一个抽象接口类,您可以将其用作基类来指定插件将其文本输出定向到何处。要创建自定义输出流,请实现一种 print
方法,以便正确处理测试框架向其传递的格式化文本信息。许多面向文本的插件都接受 OutputStream
实例,以便以可配置方式重定向其生成的文本。
matlab.unittest.plugins.OutputStream
类是 handle
类。
示例
创建自定义输出流
在当前文件夹中的一个文件中,创建名为 ToFigure
的类,该类将插件输出重定向到图窗,并将其显示在该图窗内的一个列表框中。定义 Figure
和 ListBox
属性,分别表示图窗和列表框的句柄。
classdef ToFigure < matlab.unittest.plugins.OutputStream properties(SetAccess = private) Figure end properties(Access = private) ListBox end
您必须对 OutputStream
的任何子类实现 print
方法。在此示例中,该方法新建一个图窗(如果需要),格式化传入文本,然后将其添加到输出流中。
methods function print(stream,formatSpec,varargin) % Create the figure if isempty(stream.Figure) || ~ishghandle(stream.Figure) stream.createFigure end newStr = sprintf(formatSpec,varargin{:}); oldStr = strjoin(stream.ListBox.String','\n'); % Create the full message fullStr = strjoin([oldStr,newStr]); fullStrArray = strsplit(fullStr,'\n','CollapseDelimiters',false); % Set the string and selection stream.ListBox.String = fullStrArray'; stream.ListBox.Value = numel(fullStrArray); drawnow end end
在具有 private
访问权限的 methods
代码块中,实现名为 createFigure
的辅助方法,该方法创建插件使用的图窗和列表框。
methods(Access = private) function createFigure(stream) stream.Figure = figure(... 'Name', 'Unit Test Output',... 'WindowStyle', 'docked'); stream.ListBox = uicontrol(... 'Parent', stream.Figure,... 'Style', 'listbox',... 'String', {},... 'Units', 'normalized',... 'Position', [.05 .05 .9 .9],... 'Max', 2, ... 'FontName', 'Monospaced',... 'FontSize', 13); end end end
保存 ToFigure
类。现在,在当前文件夹中,创建一个名为 ExampleTest.m
的文件,其中包含以下测试类。testOne
中的 verifyEqual
验证导致测试失败。testTwo
中的验证通过。对应于 testThree
的测试通过,但未产生输出。
classdef ExampleTest < matlab.unittest.TestCase methods(Test) function testOne(testCase) % Test fails testCase.verifyEqual(5,4,'Testing 5==4'); end function testTwo(testCase) % Test passes testCase.verifyEqual(5,5,'Testing 5==5'); end function testThree(testCase) % test code end end end
在命令提示符下,基于 ExampleTest
类创建测试套件。
import matlab.unittest.TestRunner import matlab.unittest.plugins.DiagnosticsValidationPlugin suite = testsuite('ExampleTest');
创建一个将输出显示到命令行窗口中的测试运行程序。
runner = TestRunner.withTextOutput;
创建一个 DiagnosticsValidationPlugin
实例,它显式指定应使用 ToFigure
输出流将其输出定向到图窗。
plugin = DiagnosticsValidationPlugin(ToFigure);
将该插件添加到运行程序中并运行测试。
runner.addPlugin(plugin) result = runner.run(suite);
Running ExampleTest ================================================================================ Verification failed in ExampleTest/testOne. ---------------- Test Diagnostic: ---------------- Testing 5==4 --------------------- Framework Diagnostic: --------------------- verifyEqual failed. --> The values are not equal using "isequaln". --> Failure table: Actual Expected Error RelativeError ______ ________ _____ _____________ 5 4 1 0.25 Actual Value: 5 Expected Value: 4 ------------------ Stack Information: ------------------ In C:\work\ExampleTest.m (ExampleTest.testOne) at 4 ================================================================================ ... Done ExampleTest __________ Failure Summary: Name Failed Incomplete Reason(s) ================================================================== ExampleTest/testOne X Failed by verification.
只有测试失败才在屏幕上产生输出。默认情况下,TestRunner.withTextOutput
使用 DiagnosticsOutputPlugin
在屏幕上显示输出。
除了显示在屏幕上的默认文本输出外,DiagnosticsValidationPlugin
输出还定向到停靠的图窗。该图窗显示此文本。
------------------------------ Validation of Test Diagnostic: ------------------------------ Testing 5==4 ------------------------------ Validation of Test Diagnostic: ------------------------------ Testing 5==5
DiagnosticsValidationPlugin
显示诊断信息,而不管测试是否遇到失败条件。
版本历史记录
在 R2014a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)