Main Content

log

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

Record diagnostic information during fixture setup and teardown

Description

log(f,diagnostic) logs the supplied diagnostic information. You can use the log method to log information during test fixture setup and teardown routines. 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(f,v,diagnostic) logs the diagnostic information at the specified verbosity level, v.

example

Input Arguments

expand all

Fixture, specified as a matlab.unittest.fixtures.Fixture 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, 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