Class: matlab.unittest.plugins.LoggingPlugin
Package: matlab.unittest.plugins
Construct LoggingPlugin for messages of specified verbosity
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(
constructs a v
)LoggingPlugin
for messages of the specified verbosity.
matlab.unittest.plugins.LoggingPlugin.withVerbosity(
redirects the text output to the output stream.v
,stream
)
matlab.unittest.plugins.LoggingPlugin.withVerbosity(
includes additional options specified by one or more v
,Name,Value
)Name,Value
pair arguments.
v
— Verbosity levels supported by plugin instance0
| 1
| 2
| 3
| 4
| matlab.unittest.Verbosity
enumeration | enumeration name as string or char vectorVerbosity levels supported by the plugin instance, specified as an integer value between 0 and 4, a matlab.unittest.Verbosity
enumeration object, or a string scalar or character vector corresponding to one of the predefined enumeration member names. The plugin reacts to diagnostics that are logged at this level and lower. Integer values correspond to the members of the matlab.unittest.Verbosity
enumeration.
Numeric Representation | Enumeration Member Name | Verbosity Description |
---|---|---|
0 | None | No information |
1 | Terse | Minimal information |
2 | Concise | Moderate amount of information |
3 | Detailed | Some supplemental information |
4 | Verbose | Lots of supplemental information |
stream
— Location where plugin directs text outputToStandardOutput
instance (default) | OutputStream
instanceLocation where the plugin directs text output, specified as an OutputStream
instance. By default, the plugin uses the OutputStream
subclass ToStandardOutput
as the stream.
Specify optional
comma-separated pairs of Name,Value
arguments. Name
is
the argument name and Value
is the corresponding value.
Name
must appear inside quotes. You can specify several name and value
pair arguments in any order as
Name1,Value1,...,NameN,ValueN
.
'Description'
— Logged diagnostic message description'Diagnostic logged'
(default) | character vector | string scalarLogged diagnostic message description, specified as a character vector or string scalar. This value is printed alongside each logged diagnostic message. If the value empty, the test framework does not display a description.
'ExcludingLowerLevels'
— Indicator to display messages logged at levels lower than the verbosity levelfalse
(default) | true
Indicator to display messages logged at levels lower than the verbosity level, v
, specified as false
or true
(logical(0)
or logical(1)
). By default, the value is false
and the plugin reacts to all messages logged at level v
or lower. If the value is true
, the plugin reacts only to messages logged at level v
.
'HideLevel'
— Indicator to display verbosity levelfalse
(default) | true
Indicator to display the verbosity level alongside each logged diagnostic, specified as false
or true
(logical(0)
or logical(1)
). By default, the value is false
and the test framework displays the verbosity level.
'HideTimestamp'
— Indicator to display timestampfalse
(default) | true
Indicator to display the timestamp from when the test framework generates the logged message alongside each logged diagnostic, specified as false
or true
(logical(0)
or logical(1)
). By default, the value is false
and the test framework displays the timestamp.
'NumStackFrames'
— Number of stack frames to display0
(default) | integer value | Inf
Number of stack frames to display after each logged diagnostic message, specified as an integer value. By default, the value is 0
, and the test framework does not display stack information. If NumStackFrames
is Inf
, the test framework displays all available stack frames.
Create a function-based test in a file, sampleLogTest.m
,
in your working folder.
function tests = sampleLogTest tests = functiontests(localfunctions); function svdTest(testCase) import matlab.unittest.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)
At the command prompt, run the test.
results = run(sampleLogTest);
Running sampleLogTest [Terse] Diagnostic logged (2014-04-14T14:20:59): About to call SVD. [Terse] Diagnostic logged (2014-04-14T14:20:59): SVD finished. . Done sampleLogTest __________
The default runner reports the diagnostics at level 1 (Terse
).
Create a test runner to report the diagnostics at levels 1 and 2, and rerun the test.
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 (2014-04-14T14:28:14): Generating matrix. [Terse] Diagnostic logged (2014-04-14T14:28:14): About to call SVD. [Terse] Diagnostic logged (2014-04-14T14:28:15): SVD finished.
Create the following class In a file in your current working folder, 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,matlab.unittest.Verbosity.Detailed,'Starting Test') log(testCase,'Testing 5==5') testCase.verifyEqual(5,5) log(testCase,matlab.unittest.Verbosity.Verbose,'Test Complete') end end end
The log messages in testTwo
uses Verbosity
enumerations instead of the corresponding integers.
At the command prompt, create the test suite and a runner at verbosity level 4, and then run the test.
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 (2014-04-14T15:24:03): Starting Test [Concise] Diagnostic logged (2014-04-14T15:24:03): Testing 5==4 [Verbose] Diagnostic logged (2014-04-14T15:24:03): Test Complete [Detailed] Diagnostic logged (2014-04-14T15:24:03): Starting Test [Concise] Diagnostic logged (2014-04-14T15:24:03): Testing 5==5 [Verbose] Diagnostic logged (2014-04-14T15:24:03): Test Complete
Create a new plugin to direct the output to a file, myOutput.log
, and rerun the tests.
import matlab.unittest.plugins.ToFile outFile = 'myOutput.log'; runner = TestRunner.withNoPlugins; p = LoggingPlugin.withVerbosity(4,ToFile(outFile)); runner.addPlugin(p); results = runner.run(suite);
Observe the contents in the file created by the plugin.
disp(fileread(outFile))
[Detailed] Diagnostic logged (2014-04-14T15:27:44): Starting Test [Concise] Diagnostic logged (2014-04-14T15:27:44): Testing 5==4 [Verbose] Diagnostic logged (2014-04-14T15:27:44): Test Complete [Detailed] Diagnostic logged (2014-04-14T15:27:44): Starting Test [Concise] Diagnostic logged (2014-04-14T15:27:44): Testing 5==5 [Verbose] Diagnostic logged (2014-04-14T15:27:44): Test Complete
Create a new plugin that does not display level 4 messages. Do not display the verbosity level or timestamp. Rerun the tests.
runner = TestRunner.withNoPlugins; p = LoggingPlugin.withVerbosity(matlab.unittest.Verbosity.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
log
| log
| matlab.unittest.plugins.OutputStream
| matlab.unittest.plugins.ToStandardOutput
| matlab.unittest.Verbosity