Contenuto principale

log

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

Record diagnostic information during test execution

Description

log(testCase,diagnostic) logs the supplied diagnostic information. You can use the log method to log information during test execution. The test runner displays logged messages only if you configure it to do so by adding an appropriate plugin, such as a matlab.unittest.plugins.LoggingPlugin instance.

example

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

example

Input Arguments

expand all

Test case, specified as a matlab.unittest.TestCase object.

Diagnostic information to display, specified as a string array, character array, function handle, or array of matlab.automation.diagnostics.Diagnostic objects.

Verbosity level, specified as an integer scalar from 1 through 4, a matlab.automation.Verbosity enumeration object, or a text representation of the enumeration. By default, the method uses the Concise verbosity level for diagnostic messages.

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