Main Content

matlab.unittest.plugins.LoggingPlugin.withVerbosity

类: matlab.unittest.plugins.LoggingPlugin
命名空间: matlab.unittest.plugins

为指定详细程度的消息构造 LoggingPlugin

语法

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v)
matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,stream)
matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,Name,Value)

说明

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v) 为指定详细程度的消息构造 LoggingPlugin

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,stream) 将文本输出重定向到输出流。

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,Name,Value) 包括由一个或多个 Name,Value 对组参数指定的其他选项。

输入参数

全部展开

插件实例支持的详细级别,指定为介于 0 和 4 之间的整数值、matlab.automation.Verbosity 枚举对象,或对应于预定义枚举成员名称之一的字符串标量或字符向量。该插件对在此级别和更低级别记录的诊断做出响应。整数值对应于 matlab.automation.Verbosity 枚举的成员。

数值表示枚举成员名称详细程度描述
0None

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

插件定向文本输出的位置,指定为 OutputStream 实例。默认情况下,插件使用 OutputStream 子类 ToStandardOutput 作为流。

名称-值参数

将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参数名称,Value 是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

记录的诊断消息说明,指定为字符向量或字符串标量。此值随每条记录的诊断消息一起输出。如果值为空,测试框架将不显示说明。

是否显示在低于详细级别 v 的级别记录的消息的指示符,指定为 falsetrue (logical(0)logical(1))。默认情况下,值为 false,并且该插件将对在 v 或更低级别记录的所有消息做出响应。如果值为 true,该插件将仅对在 v 级别记录的消息做出响应。

是否在每个记录诊断旁边显示详细级别的指示符,指定为 falsetruelogical(0)logical(1))。默认情况下,值为 false,并且该测试框架将显示详细级别。

是否显示测试框架在每个记录诊断旁边生成记录消息的时间戳的指示符,指定为 falsetruelogical(0)logical(1))。默认情况下,值为 false,并且该测试框架将显示时间戳。

要在每条记录的诊断消息后显示的堆栈帧数,指定为一个整数值。默认情况下,值为 0,并且该测试框架不显示堆栈信息。如果 NumStackFramesInf,则测试框架将显示所有可用的堆栈帧。

示例

全部展开

在工作文件夹下的文件 sampleLogTest.m 中,创建一个基于函数的测试。

function tests = sampleLogTest
tests = functiontests(localfunctions);

function svdTest(testCase)
import matlab.automation.Verbosity

log(testCase,'Generating matrix.')
m = rand(1000);

log(testCase,1,'About to call SVD.')
[U,S,V] = svd(m);

log(testCase,Verbosity.Terse,'SVD finished.')

verifyEqual(testCase,U*S*V',m,'AbsTol',1e-6)

在命令提示符下运行测试。

results = run(sampleLogTest);
Running sampleLogTest

[Terse] Diagnostic logged (2022-10-15 18:35:02): About to call SVD.

[Terse] Diagnostic logged (2022-10-15 18:35:20): SVD finished.
.
Done sampleLogTest
__________

默认运行器报告第 1 级别的诊断 (Terse)。

创建一个用于报告级别 1 和级别 2 的诊断的测试运行器,并重新运行该测试。

import matlab.unittest.TestRunner
import matlab.unittest.plugins.LoggingPlugin

runner = TestRunner.withNoPlugins;
p = LoggingPlugin.withVerbosity(2);
runner.addPlugin(p)

results = runner.run(sampleLogTest);
 [Concise] Diagnostic logged (2022-10-15T18:36:05): Generating matrix.
   [Terse] Diagnostic logged (2022-10-15T18:36:05): About to call SVD.
   [Terse] Diagnostic logged (2022-10-15T18:36:05): SVD finished.

在您的当前工作文件夹下的文件 ExampleLogTest.m 中,创建以下类。

classdef ExampleLogTest < matlab.unittest.TestCase
    methods(Test)
        function testOne(testCase)  % Test fails
            log(testCase,3,'Starting Test')
            log(testCase,'Testing 5==4')
            testCase.verifyEqual(5,4)
            log(testCase,4,'Test Complete')
        end
        function testTwo(testCase)  % Test passes
            log(testCase,'Detailed','Starting Test')
            log(testCase,'Testing 5==5')
            testCase.verifyEqual(5,5)
            log(testCase,'Verbose','Test Complete')
        end
    end
end

在命令提示符下,创建该测试套件和一个详细级别为 4 的运行器,然后运行该测试。

import matlab.unittest.TestSuite
import matlab.unittest.TestRunner
import matlab.unittest.plugins.LoggingPlugin
suite = TestSuite.fromClass(?ExampleLogTest);

runner = TestRunner.withNoPlugins;
p = LoggingPlugin.withVerbosity(4);
runner.addPlugin(p)

results = runner.run(suite);
[Detailed] Diagnostic logged (2022-10-15T18:45:43): Starting Test
 [Concise] Diagnostic logged (2022-10-15T18:45:43): Testing 5==4
 [Verbose] Diagnostic logged (2022-10-15T18:45:44): Test Complete
[Detailed] Diagnostic logged (2022-10-15T18:45:44): Starting Test
 [Concise] Diagnostic logged (2022-10-15T18:45:44): Testing 5==5
 [Verbose] Diagnostic logged (2022-10-15T18:45:44): Test Complete

创建一个新插件以将输出定向到文件 myOutput.log 并返回测试。

import matlab.automation.streams.ToFile
outFile = 'myOutput.log';

runner = TestRunner.withNoPlugins;
p = LoggingPlugin.withVerbosity(4,ToFile(outFile));
runner.addPlugin(p)

results = runner.run(suite);

观察该插件创建的文件中的内容。

disp(fileread(outFile))
[Detailed] Diagnostic logged (2022-10-15T18:46:09): Starting Test
 [Concise] Diagnostic logged (2022-10-15T18:46:09): Testing 5==4
 [Verbose] Diagnostic logged (2022-10-15T18:46:09): Test Complete
[Detailed] Diagnostic logged (2022-10-15T18:46:09): Starting Test
 [Concise] Diagnostic logged (2022-10-15T18:46:09): Testing 5==5
 [Verbose] Diagnostic logged (2022-10-15T18:46:09): Test Complete

创建一个不显示第 4 级别的消息的新插件。不显示详细级别或时间戳。重新运行测试。

runner = TestRunner.withNoPlugins;
p = LoggingPlugin.withVerbosity('Detailed', ...
    'HideLevel',true,'HideTimestamp',true);
runner.addPlugin(p)

results = runner.run(suite);
Diagnostic logged: Starting Test
Diagnostic logged: Testing 5==4
Diagnostic logged: Starting Test
Diagnostic logged: Testing 5==5

版本历史记录

在 R2014b 中推出