メインコンテンツ

selectPassed

クラス: matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord
名前空間: matlab.unittest.plugins.diagnosticrecord

パスしたイベントの診断記録を返す

構文

selectedRecords = selectPassed(records)

説明

selectedRecords = selectPassed(records) は、パスしたイベントの診断記録を matlab.unittest.plugins.diagnosticrecord.QualificationDiagnosticRecord インスタンスの配列として返します。

入力引数

すべて展開する

テスト結果の診断記録。matlab.unittest.plugins.diagnosticrecord.DiagnosticRecord インスタンスの配列として指定します。TestResultDetails プロパティの DiagnosticRecord フィールドを介して診断記録にアクセスします。たとえば、テスト結果が変数 results に格納されている場合、2 番目のテストの診断記録を検索するには records = result(2).Details.DiagnosticRecord を呼び出します。

すべて展開する

現在のフォルダー内の ExampleTest.m という名前のファイルで、ExampleTest テスト クラスを作成します。このクラスの Test メソッドは、説明をわかりやすくすることを意図した内容になっています。testOne メソッドには、Terse レベルおよび Detailed レベルでログに記録される診断とパスおよび失敗する検定が含まれています。testTwo メソッドには意図的なバグがあり、テストにおいて変数ではなく文字を 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);

2 番目のテストの結果を表示します。テスト コードに意図的なエラーがあるため、テストは失敗して未完了のままになります。

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 フィールドを使用して、2 番目のテストについての記録された診断にアクセスします。記録には、キャッチされないエラーがテストでスローされたことが示されています。

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 で導入