Main Content

log

Class: matlab.unittest.fixtures.Fixture
Namespace: matlab.unittest.fixtures

Record diagnostic information during fixture setup and teardown

Syntax

log(f,diagnostic)
log(f,v,diagnostic)

Description

log(f,diagnostic) logs the supplied diagnostic. The log method provides a means for tests to log information during fixture setup and teardown routines. 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(f,v,diagnostic) logs the diagnostic at the specified verbosity level, v.

Input Arguments

expand all

Instance of fixture, specified as a matlab.unittest.fixtures.Fixture.

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

Verbosity level, specified as an integer value between 1 and 4 or a matlab.automation.Verbosity enumeration object. 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, FormatHexFixture.m, in your current working folder, create the following fixture.

classdef FormatHexFixture < matlab.unittest.fixtures.Fixture
    properties (Access=private)
        OriginalFormat
    end
    methods
        function setup(fixture)
            fixture.OriginalFormat = format().NumericFormat;
            fixture.log(['The previous format setting was ',...
                fixture.OriginalFormat])
            log(fixture,'Setting Format')
            format('hex')
            log(fixture,3,'Format Set')
        end
        function teardown(fixture)
            log(fixture,'Resetting Format')
            format(fixture.OriginalFormat)
            log(fixture,3,'Original Format Restored')
        end
    end
end

In a file, SampleTest.m, in your current working folder, create the following test class.

classdef SampleTest < matlab.unittest.TestCase
    methods (Test)
        function test1(testCase)
            testCase.applyFixture(FormatHexFixture);
            actStr = getColumnForDisplay([1;2;3], 'Small Integers');
            expStr = ['Small Integers  '
                '3ff0000000000000'
                '4000000000000000'
                '4008000000000000'];
            testCase.verifyEqual(actStr, expStr)
        end
    end
end

function str = getColumnForDisplay(values, title)
elements = cell(numel(values)+1, 1);
elements{1} = title;
for idx = 1:numel(values)
    elements{idx+1} = displayNumber(values(idx));
end
str = char(elements);
end

function str = displayNumber(n)
str = strtrim(evalc('disp(n);'));
end

Run the test.

result = run(SampleTest);
Running SampleTest
.
Done SampleTest
__________

None of the logged messages are displayed because the default test runner has a verbosity level of 1 (Terse) and the default log message is at level 2 (Concise).

Create a test runner to report the diagnostics at levels 1, 2, and 3 and rerun the test.

import matlab.unittest.TestRunner
import matlab.unittest.plugins.LoggingPlugin

ts = matlab.unittest.TestSuite.fromClass(?SampleTest);
runner = TestRunner.withNoPlugins;
p = LoggingPlugin.withVerbosity(3);
runner.addPlugin(p);

results = runner.run(ts);
 [Concise] Diagnostic logged (2022-09-30T15:36:46):
The previous format setting was 
short

 [Concise] Diagnostic logged (2022-09-30T15:36:46): Setting Format
[Detailed] Diagnostic logged (2022-09-30T15:36:46): Format Set
 [Concise] Diagnostic logged (2022-09-30T15:36:47): Resetting Format
[Detailed] Diagnostic logged (2022-09-30T15:36:47): Original Format Restored

Version History

Introduced in R2014b