selectLogged
类: matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord
命名空间: matlab.unittest.plugins.diagnosticrecord
返回日志记录事件的诊断记录
语法
selectedRecords = selectLogged(records)
说明
selectedRecords = selectLogged(
以 records
)matlab.unittest.plugins.diagnosticrecord.LoggedDiagnosticRecord
实例的数组形式返回日志记录事件的诊断记录。日志记录事件是指在测试中对 log
方法的调用。
输入参数
记录的测试结果诊断信息,以 matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord
实例的数组形式指定。通过 TestResult
中的 Details
属性的 DiagnosticRecord
字段访问记录的诊断信息。例如,如果您的测试结果存储在变量 results
中,通过调用 records = result(2).Details.DiagnosticRecord
找到为第二个测试记录的诊断信息。
示例
在当前文件夹的名为 ExampleTest.m
的文件中创建 ExampleTest
测试类。类中 Test
方法的内容仅用于说明目的。testOne
方法包括在 Terse
和 Detailed
级别进行日志记录的诊断信息以及通过和失败的鉴定。testTwo
方法包含特意设置的 Bug - 测试向 ones
函数传递字符而不是变量。
classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testOne(testCase) testCase.log(1,"Terse log message") % logs testCase.log(3,"Detailed log message") % logs testCase.verifyEqual(3+2,5) % passes testCase.assumeTrue(true) % passes testCase.verifyGreaterThan(5,9) % fails testCase.assertEqual(3.14,pi) % fails/incomplete end function testTwo(testCase) a = [1 2]; testCase.verifyEqual(ones('a'),[1 1]) % errors end end end
导入 DiagnosticsRecordingPlugin
类。
import matlab.unittest.plugins.DiagnosticsRecordingPlugin
基于测试类创建一个测试套件。
suite = testsuite("ExampleTest");
创建一个测试运行器并使用 DiagnosticsRecordingPlugin
实例配置它。然后,运行测试。插件记录有关测试结果的诊断信息。
runner = testrunner("minimal");
plugin = DiagnosticsRecordingPlugin;
runner.addPlugin(plugin)
results = runner.run(suite);
显示第二个测试的结果。由于测试代码中存在特意设置的错误,测试失败并保持未完成状态。
results(2)
ans = TestResult with properties: Name: 'ExampleTest/testTwo' Passed: 0 Failed: 1 Incomplete: 1 Duration: 8.0680e-04 Details: [1×1 struct] Totals: 0 Passed, 1 Failed (rerun), 1 Incomplete. 0.0008068 seconds testing time.
使用 TestResult
对象的 Details
属性中的 DiagnosticRecord
字段访问第二次测试的所记录的诊断信息。记录指示测试引发了未捕获的错误。
results(2).Details.DiagnosticRecord
ans = ExceptionDiagnosticRecord with properties: Event: 'ExceptionThrown' EventScope: TestMethod EventLocation: 'ExampleTest/testTwo' Exception: [1×1 MException] AdditionalDiagnosticResults: [1×0 matlab.automation.diagnostics.DiagnosticResult] Stack: [1×1 struct] Report: 'Error occurred in ExampleTest/testTwo and it did not run to completion.↵ ...'
查看插件为 testOne
记录的事件。默认情况下,DiagnosticsRecordingPlugin
实例仅记录失败事件和在 matlab.automation.Verbosity.Terse
级别进行日志记录的事件的诊断信息。
testOneRecords = results(1).Details.DiagnosticRecord; {testOneRecords.Event}'
ans = 3×1 cell array {'DiagnosticLogged' } {'VerificationFailed'} {'AssertionFailed' }
现在,使用在 matlab.automation.Verbosity.Detailed
级别记录所有事件(即通过事件、失败事件和在任何级别进行日志记录的事件)详细信息的插件运行测试。
runner = testrunner("minimal"); plugin = DiagnosticsRecordingPlugin( ... IncludingPassingDiagnostics=true, ... LoggingLevel="Verbose", ... OutputDetail="Detailed"); runner.addPlugin(plugin) results = runner.run(suite);
查看插件为 testOne
记录的事件。该插件记录所有鉴定的诊断信息以及所有调用 log
方法获得的诊断信息。
testOneRecords = results(1).Details.DiagnosticRecord; {testOneRecords.Event}'
ans = 6×1 cell array {'DiagnosticLogged' } {'DiagnosticLogged' } {'VerificationPassed'} {'AssumptionPassed' } {'VerificationFailed'} {'AssertionFailed' }
返回 testOne
中失败事件的诊断记录。
failedRecords = selectFailed(testOneRecords)
failedRecords = 1×2 QualificationDiagnosticRecord array with properties: Event EventScope EventLocation TestDiagnosticResults FrameworkDiagnosticResults AdditionalDiagnosticResults Stack Report
返回通过事件的记录,并显示第一个记录的报告。
passedRecords = selectPassed(testOneRecords); passedRecords(1).Report
ans = 'Verification passed in ExampleTest/testOne. --------------------- Framework Diagnostic: --------------------- verifyEqual passed. --> The numeric values are equal using "isequaln". Actual Value: 5 Expected Value: 5 ------------------ Stack Information: ------------------ In C:\work\ExampleTest.m (ExampleTest.testOne) at 6'
返回 testOne
中任何未完成事件的记录。由于测试中的未完成事件是断言失败引起的,测试框架还包括此记录作为失败事件记录 (failedRecords(2)
) 的一部分。
incompleteRecords = selectIncomplete(testOneRecords)
incompleteRecords = QualificationDiagnosticRecord with properties: Event: 'AssertionFailed' EventScope: TestMethod EventLocation: 'ExampleTest/testOne' TestDiagnosticResults: [1×0 matlab.automation.diagnostics.DiagnosticResult] FrameworkDiagnosticResults: [1×1 matlab.automation.diagnostics.DiagnosticResult] AdditionalDiagnosticResults: [1×0 matlab.automation.diagnostics.DiagnosticResult] Stack: [1×1 struct] Report: 'Assertion failed in ExampleTest/testOne and it did not run to completion.↵ ...'
返回具有日志记录事件的记录,并显示日志记录的消息。
loggedRecords = selectLogged(testOneRecords); {loggedRecords.Report}'
ans = 2×1 cell array {'[Terse] Diagnostic logged (2024-08-21 17:00:01): Terse log message' } {'[Detailed] Diagnostic logged (2024-08-21 17:00:01): Detailed log message'}
版本历史记录
在 R2016a 中推出
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.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)