Main Content

matlab.unittest.plugins.LoggingPlugin.withVerbosity

Class: matlab.unittest.plugins.LoggingPlugin
Package: matlab.unittest.plugins

Construct LoggingPlugin for messages of specified verbosity

Syntax

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v)
matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,stream)
matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,Name,Value)

Description

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v) constructs a LoggingPlugin for messages of the specified verbosity.

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,stream) redirects the text output to the output stream.

matlab.unittest.plugins.LoggingPlugin.withVerbosity(v,Name,Value) includes additional options specified by one or more Name,Value pair arguments.

Input Arguments

expand all

Verbosity 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 RepresentationEnumeration Member NameVerbosity Description
0None

No information

1Terse

Minimal information

2Concise

Moderate amount of information

3Detailed

Some supplemental information

4Verbose

Lots of supplemental information

Location where the plugin directs text output, specified as an OutputStream instance. By default, the plugin uses the OutputStream subclass ToStandardOutput as the stream.

Name-Value Arguments

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.

Logged 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.

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.

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.

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.

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.

Examples

expand all

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
Introduced in R2014b