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(
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 f
,diagnostic
)matlab.unittest.plugins.LoggingPlugin
.
log(
logs
the diagnostic at the specified verbosity level, f
,v
,diagnostic
)v
.
Input Arguments
f
— Instance of fixture
matlab.unittest.fixtures.Fixture
instance
Instance of fixture, specified as a matlab.unittest.fixtures.Fixture
.
diagnostic
— Diagnostic information to display upon a failure
string | character vector | function handle | matlab.automation.diagnostics.Diagnostic
instance
Diagnostic information to display upon a failure, specified as a string, character vector,
function handle, or
matlab.automation.diagnostics.Diagnostic
instance.
v
— Verbosity level
2
(default) | 1
| 3
| 4
| matlab.automation.Verbosity
enumeration
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 Representation | Enumeration Member Name | Verbosity Description |
---|---|---|
1 | Terse | Minimal information |
2 | Concise | Moderate amount of information |
3 | Detailed | Some supplemental information |
4 | Verbose | Lots of supplemental information |
Examples
Log Diagnostic Information
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
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)