Main Content

selectIncomplete

Class: matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord
Namespace: matlab.unittest.plugins.diagnosticrecord

Return diagnostic records for incomplete events

Syntax

selectedRecords = selectIncomplete(records)

Description

selectedRecords = selectIncomplete(records) returns the diagnostic records for incomplete events as an array of matlab.unittest.plugins.diagnosticrecord.QualificationDiagnosticRecord and matlab.unittest.plugins.diagnosticrecord.ExceptionDiagnosticRecord instances.

Incomplete events are events that result in an incomplete test on the TestResult. These events include assumption failures, assertion failures, and uncaught MException objects.

Input Arguments

expand all

Recorded diagnostics on a test result, specified as an array of matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord instances. Access recorded diagnostics via the DiagnosticRecord field in the Details property on TestResult. For example, if your test results are stored in the variable results, find the recorded diagnostics for the second test by invoking records = result(2).Details.DiagnosticRecord.

Examples

expand all

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.

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

Import the DiagnosticsRecordingPlugin class.

import matlab.unittest.plugins.DiagnosticsRecordingPlugin

Create a test suite from the test class.

suite = testsuite("ExampleTest");

Create a test runner and configure it using a DiagnosticsRecordingPlugin instance. Then, run the tests. The plugin records diagnostics on the test results.

runner = testrunner("minimal");
plugin = DiagnosticsRecordingPlugin;
runner.addPlugin(plugin)
results = runner.run(suite);

Display the result of the second test. Due to the intentional error in the test code, the test fails and remains incomplete.

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.

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.

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.↵ ...'

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.

testOneRecords = results(1).Details.DiagnosticRecord;
{testOneRecords.Event}'
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.

runner = testrunner("minimal");
plugin = DiagnosticsRecordingPlugin( ...
    IncludingPassingDiagnostics=true, ...
    LoggingLevel="Verbose", ...
    OutputDetail="Detailed");
runner.addPlugin(plugin)
results = runner.run(suite);

View the events that the plugin recorded for testOne. The plugin recorded diagnostic information for all the qualifications and calls to the log method.

testOneRecords = results(1).Details.DiagnosticRecord;
{testOneRecords.Event}'
ans =

  6×1 cell array

    {'DiagnosticLogged'  }
    {'DiagnosticLogged'  }
    {'VerificationPassed'}
    {'AssumptionPassed'  }
    {'VerificationFailed'}
    {'AssertionFailed'   }

Return the diagnostic records for failed events in testOne.

failedRecords = selectFailed(testOneRecords)
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.

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'

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 = 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.↵ ...'

Return the records for logged events, and display the logged messages.

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'}

Version History

Introduced in R2016a