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
记录诊断信息。
输入参数
f
— 脚手架实例
matlab.unittest.fixtures.Fixture
实例
脚手架实例,指定为 matlab.unittest.fixtures.Fixture
。
diagnostic
— 失败时显示的诊断信息
字符串 | 字符向量 | 函数句柄 | matlab.automation.diagnostics.Diagnostic
实例
失败时显示的诊断信息,指定为字符串、字符向量、函数句柄或 matlab.automation.diagnostics.Diagnostic
实例。
v
— 详细级别
2
(默认) | 1
| 3
| 4
| matlab.automation.Verbosity
枚举
详细级别,指定为介于 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 命令
您点击的链接对应于以下 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)