Main Content

log

Class: matlab.unittest.TestCase
Namespace: matlab.unittest

Record diagnostic information during test execution

Syntax

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

Description

log(testCase,diagnostic) logs the supplied diagnostic. The log method provides a means for tests to log information during their execution. The testing framework displays logged messages only if you configure it to do so by adding an appropriate plugin, such as the matlab.unittest.plugins.LoggingPlugin.

log(testCase,v,diagnostic) logs the diagnostic at the specified verbosity level, v.

Input Arguments

expand all

Instance of test case, specified as a matlab.unittest.TestCase.

Diagnostic information to display upon a failure, specified as a string array, character array, function handle, or matlab.automation.diagnostics.Diagnostic instance.

Verbosity level, specified as an integer value between 1 and 4, a matlab.automation.Verbosity enumeration object, or a string scalar or character vector corresponding to one of the predefined enumeration member names. The default verbosity level for diagnostic messages is Concise. Integer values correspond to the members of the matlab.automation.Verbosity enumeration.

Numeric RepresentationEnumeration Member NameVerbosity Description
1Terse

Minimal information

2Concise

Moderate amount of information

3Detailed

Some supplemental information

4Verbose

Lots of supplemental information

Examples

expand all

In a file named sampleTest.m in your current folder, create a function-based test that includes logged diagnostics.

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

Import the LoggingPlugin class.

import matlab.unittest.plugins.LoggingPlugin

Create a test suite from the test file.

suite = testsuite("sampleTest.m");

Run the test using a default test runner. The test runner displays diagnostics logged at the matlab.automation.Verbosity.Terse level (level 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
__________

Create a new test runner and configure it using a plugin that displays diagnostics logged at or below the matlab.automation.Verbosity.Concise level (level 2). Then, rerun the test using the test runner. The plugin displays all the logged diagnostics associated with the test.

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

Version History

Introduced in R2014b