matlab.automation.streams.OutputStream 类
命名空间: matlab.automation.streams
描述
matlab.automation.streams.OutputStream 类提供一个接口,您可以使用该接口来指定插件将其文本输出定向到何处。要创建一个自定义输出流,请实现 print 方法,该方法处理向其传递的格式化文本信息。许多面向文本的插件都接受 OutputStream 实例,以便以可配置方式重定向其生成的文本。
matlab.automation.streams.OutputStream 类是 handle 类。
示例
在当前文件夹中的一个文件中,创建名为 ToFigure 的类,该类将插件输出重定向到图窗,并将其显示在该图窗内的一个列表框中。定义 Figure 和 ListBox 属性,分别表示图窗和列表框的句柄。
classdef ToFigure < matlab.automation.streams.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 numeric 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.unittest.plugins.OutputStream 现在命名为 matlab.automation.streams.OutputStream。此类的行为保持不变,代码中的现有 matlab.unittest.plugins.OutputStream 实例继续按预期工作。目前没有删除对 matlab.unittest.plugins.OutputStream 的现有实例支持的计划。
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)