log
类: matlab.unittest.fixtures.Fixture
命名空间: matlab.unittest.fixtures
记录在脚手架设置和拆解过程中的诊断信息
语法
log(f,diagnostic)
log(f,v,diagnostic)
说明
log(
记录提供的诊断信息。日志方法提供了一种测试手段,可在脚手架设置和拆解期间记录信息。测试框架仅在经过适当配置(添加相应的插件,例如 f
,diagnostic
)matlab.unittest.plugins.LoggingPlugin
)后,才会显示记录的消息。
log(
按指定的详细级别 f
,v
,diagnostic
)v
记录诊断信息。
输入参数
脚手架实例,指定为 matlab.unittest.fixtures.Fixture
。
失败时显示的诊断信息,指定为字符串、字符向量、函数句柄或 matlab.automation.diagnostics.Diagnostic
实例。
详细级别,指定为介于 1 和 4 之间的整数值或 matlab.automation.Verbosity
枚举对象。诊断消息的默认详细级别是 Concise
。整数值对应于 matlab.automation.Verbosity
枚举的成员。
数值表示 | 枚举成员名称 | 详细程度描述 |
---|---|---|
1 | Terse | 最少的信息 |
2 | Concise | 适中信息量 |
3 | Detailed | 部分补充信息 |
4 | Verbose | 大量补充信息 |
示例
在您的当前工作文件夹下的文件 FormatHexFixture.m
中,创建以下脚手架。
classdef FormatHexFixture < matlab.unittest.fixtures.Fixture properties (Access=private) OriginalFormat end methods function setup(fixture) fixture.OriginalFormat = format().NumericFormat; fixture.log(['The previous format setting was ',... fixture.OriginalFormat]) log(fixture,'Setting Format') format('hex') log(fixture,3,'Format Set') end function teardown(fixture) log(fixture,'Resetting Format') format(fixture.OriginalFormat) log(fixture,3,'Original Format Restored') end end end
在您的当前工作文件夹下的文件 SampleTest.m
中,创建以下测试类。
classdef SampleTest < matlab.unittest.TestCase methods (Test) function test1(testCase) testCase.applyFixture(FormatHexFixture); actStr = getColumnForDisplay([1;2;3], 'Small Integers'); expStr = ['Small Integers ' '3ff0000000000000' '4000000000000000' '4008000000000000']; testCase.verifyEqual(actStr, expStr) end end end function str = getColumnForDisplay(values, title) elements = cell(numel(values)+1, 1); elements{1} = title; for idx = 1:numel(values) elements{idx+1} = displayNumber(values(idx)); end str = char(elements); end function str = displayNumber(n) str = strtrim(evalc('disp(n);')); end
运行测试。
result = run(SampleTest);
Running SampleTest . Done SampleTest __________
已记录的消息都没有显示,因为默认测试运行器的详细级别为 1 (Terse
),而默认日志消息的级别为 2 (Concise
)。
创建一个用于报告级别 1、级别 2 和级别 3 的诊断的测试运行器,并重新运行该测试。
import matlab.unittest.TestRunner import matlab.unittest.plugins.LoggingPlugin ts = matlab.unittest.TestSuite.fromClass(?SampleTest); runner = TestRunner.withNoPlugins; p = LoggingPlugin.withVerbosity(3); runner.addPlugin(p); results = runner.run(ts);
[Concise] Diagnostic logged (2022-09-30T15:36:46): The previous format setting was short [Concise] Diagnostic logged (2022-09-30T15:36:46): Setting Format [Detailed] Diagnostic logged (2022-09-30T15:36:46): Format Set [Concise] Diagnostic logged (2022-09-30T15:36:47): Resetting Format [Detailed] Diagnostic logged (2022-09-30T15:36:47): Original Format Restored
版本历史记录
在 R2014b 中推出
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)