主要内容

log

类: matlab.unittest.TestCase
命名空间: matlab.unittest

在测试执行期间记录诊断信息

说明

log(testCase,diagnostic) 记录提供的诊断信息。您可以使用 log 方法在测试执行期间记录信息。测试运行器仅在您通过添加适当的插件(例如 matlab.unittest.plugins.LoggingPlugin 实例)对其进行配置后才显示记录的消息。

示例

log(testCase,v,diagnostic) 按指定的详细级别 v 记录诊断信息。

示例

输入参数

全部展开

测试用例,指定为 matlab.unittest.TestCase 对象。

要显示的诊断信息,指定为字符串数组、字符数组、函数句柄或 matlab.automation.diagnostics.Diagnostic 对象数组。

详细级别,指定为从 14 的整数标量、matlab.automation.Verbosity 枚举对象或枚举的文本表示。默认情况下,该方法对诊断消息使用 Concise 详细级别。

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

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

示例

全部展开

在当前文件夹中名为 sampleTest.m 的文件中,创建一个基于函数的测试,其中包含记录的诊断。

function tests = sampleTest
tests = functiontests(localfunctions);
end

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)
end

导入 LoggingPlugin 类。

import matlab.unittest.plugins.LoggingPlugin

基于测试文件创建一个测试套件。

suite = testsuite("sampleTest.m");

使用默认测试运行器运行测试。测试运行器显示在 matlab.automation.Verbosity.Terse 类别(级别 1)记录的诊断。

runner = testrunner;
results = runner.run(suite);
Running sampleTest

[Terse] Diagnostic logged (2024-08-16 17:11:33): About to call SVD

[Terse] Diagnostic logged (2024-08-16 17:11:33): SVD finished
.
Done sampleTest
__________

创建一个新的测试运行器,并使用一个插件对其进行配置,该插件显示在 matlab.automation.Verbosity.Concise 类别(级别 2)或更低级别记录的诊断。然后,使用该测试运行器重新运行测试。该插件显示与测试关联的所有记录的诊断。

runner = testrunner("minimal");
plugin = LoggingPlugin.withVerbosity("Concise");
runner.addPlugin(plugin)
results = runner.run(suite);
 [Concise] Diagnostic logged (2024-08-16T17:13:11): Generating matrix
   [Terse] Diagnostic logged (2024-08-16T17:13:11): About to call SVD
   [Terse] Diagnostic logged (2024-08-16T17:13:11): SVD finished

版本历史记录

在 R2014b 中推出