In a file named ExampleTest.m
in your current folder, create the
ExampleTest
test class. The contents of the Test
methods in the class are for illustrative purposes. The testOne
method
includes diagnostics logged at the Terse
and Detailed
levels and qualifications that pass and fail. The testTwo
method
includes an intentional bug—the test passes a character instead of a variable to the
ones
function.
Import the DiagnosticsRecordingPlugin
class.
Create a test suite from the test class.
Create a test runner and configure it using a
DiagnosticsRecordingPlugin
instance. Then, run the tests. The plugin
records diagnostics on the test results.
Display the result of the second test. Due to the intentional error in the test code,
the test fails and remains incomplete.
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.
Access the recorded diagnostic for the second test using the
DiagnosticRecord
field in the Details
property of the TestResult
object. The record indicates that the test
threw an uncaught error.
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.↵ ...'
View the events that the plugin recorded for testOne
. By default, a
DiagnosticsRecordingPlugin
instance records only diagnostics for
failing events and events logged at the
matlab.automation.Verbosity.Terse
level.
ans =
3×1 cell array
{'DiagnosticLogged' }
{'VerificationFailed'}
{'AssertionFailed' }
Now, run the tests using a plugin that records the details of all events (that is,
passing events, failing events, and events logged at any level) at the
matlab.automation.Verbosity.Detailed
level.
View the events that the plugin recorded for testOne
. The plugin
recorded diagnostic information for all the qualifications and calls to the
log
method.
ans =
6×1 cell array
{'DiagnosticLogged' }
{'DiagnosticLogged' }
{'VerificationPassed'}
{'AssumptionPassed' }
{'VerificationFailed'}
{'AssertionFailed' }
Return the diagnostic records for failed events in testOne
.
failedRecords =
1×2 QualificationDiagnosticRecord array with properties:
Event
EventScope
EventLocation
TestDiagnosticResults
FrameworkDiagnosticResults
AdditionalDiagnosticResults
Stack
Report
Return the records for passed events, and display the report for the first
record.
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'
Return the records for any incomplete events in testOne
. Because the
incomplete event in the test is due to an assertion failure, the testing framework also
includes this record as part of the records for failed events
(failedRecords(2)
).
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.↵ ...'
Return the records for logged events, and display the logged messages.
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'}