主要内容

log

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

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

语法

log(testCase,diagnostic)
log(testCase,v,diagnostic)

说明

log(testCase,diagnostic) 记录提供的诊断信息。日志方法提供一种测试手段,用于在测试执行期间记录信息。测试框架仅在经过适当配置(添加相应的插件,例如 matlab.unittest.plugins.LoggingPlugin)后,才会显示记录的消息。

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

输入参数

全部展开

测试用例的实例,指定为 matlab.unittest.TestCase

失败时显示的诊断信息,指定为字符串数组、字符数组、函数句柄或 matlab.automation.diagnostics.Diagnostic 实例。

详细级别,指定为介于 1 和 4 之间的整数值、matlab.automation.Verbosity 枚举对象,或对应于预定义枚举成员名称之一的字符串标量或字符向量。诊断消息的默认详细级别是 Concise。整数值对应于 matlab.automation.Verbosity 枚举的成员。

数值表示枚举成员名称详细程度描述
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 中推出