Main Content

matlab.unittest.diagnostics.FigureDiagnostic クラス

名前空間: matlab.unittest.diagnostics

指定した Figure を保存する診断

説明

FigureDiagnostic クラスを使用して、ファイルに Figure を保存する診断を作成します。MATLAB® がテストの実行を完了した後もファイルは存続するため、ファイルをテスト後の検査に使用できます。

構築

FigureDiagnostic(fig) は、指定した Figure を保存する診断を作成します。テスト フレームワークは、FigureDiagnostic インスタンスを診断するときに fig を FIG ファイルおよび PNG ファイルに保存します。各ファイルは、接頭辞 (既定は 'Figure_')、自動生成された識別子、およびファイル拡張子から構成される一意の名前をもちます。ファイル名の例は、Figure_cf95fe7f-5a7c-4310-9c19-16c0c17a969f.png です。ファイルの場所を表示するには、FileArtifact インスタンスを介して TestResult オブジェクトにアクセスします。

FigureDiagnostic(fig,Name,Value) は、1 つ以上の Name,Value のペアの引数で指定された追加オプションを使用して診断を作成します。Name1,Value1,...,NameN,ValueN のように、複数の名前と値のペアの引数を任意の順番で指定できます。たとえば、FigureDiagnostic(fig,'Prefix','LoggedFigure_','Formats','png')fig を PNG ファイルのみとして保存し、接頭辞に 'Figure_' ではなく 'LoggedFigure_' を使用します。

入力引数

すべて展開する

テスト フレームワークが FigureDiagnostic インスタンスを診断するときに保存する Figure。関数 figure または uifigure で作成された Figure オブジェクトとして指定します。

名前と値の引数

引数のオプションのペアを Name1=Value1,...,NameN=ValueN として指定します。ここで Name は引数名で、Value は対応する値です。名前と値の引数は他の引数の後になければなりませんが、ペアの順序は重要ではありません。

R2021a より前では、コンマを使用してそれぞれの名前と値を区切り、Name を引用符で囲みます。

例: FigureDiagnostic(testFig,'Formats','fig')

Figure のファイル形式。["fig" "png"]"fig" または "png" として指定します。これらの値は、{'fig','png'}'fig' または 'png' のように文字ベクトルとして指定できます。MATLAB は、これらをオブジェクト内に string として保存します。

ファイル名の接頭辞。テキストとして指定します。接頭辞を指定しない場合、既定の接頭辞は 'Figure_' です。値を文字ベクトルまたは string スカラーとして指定します。MATLAB は、これをオブジェクト内に文字ベクトルとして保存します。

例: 'LoggedFigure_'

例: "TestFig-"

プロパティ

すべて展開する

テスト フレームワークが FigureDiagnostic インスタンスを診断するときに保存する Figure。Figure オブジェクトとして返されます。Figure プロパティは読み取り専用で、その値は作成中に設定されます。

保存した Figure のファイル形式。["fig" "png"]"fig" または "png" として返されます。Formats プロパティは読み取り専用で、その値は作成中に設定されます。

ファイル名の接頭辞。文字ベクトルとして返されます。既定の接頭辞は 'Figure_' です。Prefix プロパティは読み取り専用で、その値は作成中に設定されます。

コピーのセマンティクス

ハンドル。コピー操作に対するハンドル クラスの影響については、オブジェクトのコピーを参照してください。

すべて折りたたむ

対話型で使用する TestCase を作成します。

import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;

Figure を作成します。

fig = figure;
ax = axes(fig);
surf(ax,peaks)

FigureDiagnostic を使用して Figure をテスト診断として保存します。Figure に子がないことを確認します。この検定は失敗し、MATLAB はテスト診断を表示します。

import matlab.unittest.diagnostics.FigureDiagnostic
testCase.verifyEmpty(fig.Children,FigureDiagnostic(fig))
Interactive verification failed.

----------------
Test Diagnostic:
----------------
Figure saved to:
--> C:\work\Temp\Figure_0b3da19f-5248-442b-aebf-3fb6d707fd1b.fig
--> C:\work\Temp\Figure_0b3da19f-5248-442b-aebf-3fb6d707fd1b.png

---------------------
Framework Diagnostic:
---------------------
verifyEmpty failed.
--> The value must be empty.
--> The value has a size of [1  1].

Actual matlab.graphics.axis.Axes:
      Axes with properties:
    
                 XLim: [0 50]
                 YLim: [0 60]
               XScale: 'linear'
               YScale: 'linear'
        GridLineStyle: '-'
             Position: [0.130000000000000 0.110000000000000 0.775000000000000 0.815000000000000]
                Units: 'normalized'
    
      Use get to show all properties

対話型で使用する TestCase を作成します。

import matlab.unittest.TestCase
testCase = TestCase.forInteractiveUse;

Figure を作成します。

fig = uifigure;
ax = axes(fig);
surf(ax,membrane(6))

FigureDiagnostic を使用して、テスト診断として Figure のログを記録します。PNG ファイルのみを保存し、ファイル名にカスタム接頭辞を使用します。

import matlab.unittest.diagnostics.FigureDiagnostic
testCase.log(FigureDiagnostic(fig,'Formats','png','Prefix','LoggedFigure_'))
Interactive diagnostic logged.
Figure saved to:
--> C:\work\Temp\LoggedFigure_0a02faa1-3e14-4783-9954-b56caa6b326d.png

現在の作業フォルダーのファイル内に、FigurePropTest テスト クラスを作成します。failingTest テスト メソッドは、失敗した場合 (この例では常に失敗する) に、後で確認できるように FigureDiagnostic を使用して Figure を保存します。

classdef FigurePropTest < matlab.unittest.TestCase
    properties
        TestFigure
    end
    methods (TestMethodSetup)
        function createFigure(testCase)
            testCase.TestFigure = figure;
        end
    end
    methods (TestMethodTeardown)
        function closeFigure(testCase)
            close(testCase.TestFigure)
        end
    end
    methods (Test)
        function defaultCurrentPoint(testCase)
            cp = testCase.TestFigure.CurrentPoint;
            testCase.verifyEqual(cp,[0 0], ...
                'Default current point is incorrect')
        end
        function defaultCurrentObject(testCase)
            import matlab.unittest.constraints.IsEmpty
            co = testCase.TestFigure.CurrentObject;
            testCase.verifyThat(co,IsEmpty, ...
                'Default current object should be empty')
        end
        function failingTest(testCase)
            import matlab.unittest.diagnostics.FigureDiagnostic
            fig = testCase.TestFigure;
            ax = axes(fig);
            surf(ax,peaks)
            testCase.verifyEmpty(testCase.TestFigure.Children, ...
                FigureDiagnostic(testCase.TestFigure))
        end
    end
end

コマンド プロンプトでテスト スイートを作成します。

suite = testsuite('FigurePropTest');

診断を記録して PDF レポートを生成するサイレント テスト ランナーを作成します。

import matlab.unittest.plugins.DiagnosticsRecordingPlugin
import matlab.unittest.plugins.TestReportPlugin
runner = matlab.unittest.TestRunner.withNoPlugins;
runner.addPlugin(DiagnosticsRecordingPlugin)
runner.addPlugin(TestReportPlugin.producingPDF('MyTestReport.pdf'))

既定のアーティファクトのルートを現在の作業フォルダーに変更します。

runner.ArtifactsRootFolder = pwd;

テストを実行します。3 番目のテストは失敗します。

results = runner.run(suite)
Generating test report. Please wait.
    Preparing content for the test report.
    Adding content to the test report.
    Writing test report to file.
Test report has been saved to:
 C:\wok\MyTestReport.pdf

results = 

  1×3 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 1 Failed (rerun), 0 Incomplete.
   8.3355 seconds testing time.

3 番目のテストの診断結果を表示します。テスト フレームワークは、3 番目のテストに関連する 2 つのアーティファクトを保存しました。FigureDiagnostic オブジェクトは、既定で Figure を PNG ファイルと FIG ファイルの両方として保存します。

results(3).Details.DiagnosticRecord.TestDiagnosticResults
ans = 

  DiagnosticResult with properties:

         Artifacts: [1×2 matlab.automation.diagnostics.FileArtifact]
    DiagnosticText: 'Figure saved to:↵--> C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig↵--> C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.png'

最初のアーティファクトの保存場所を表示します。

results(3).Details.DiagnosticRecord.TestDiagnosticResults.Artifacts(1)
ans = 

  FileArtifact with properties:

        Name: "Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig"
    Location: "C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb"
    FullPath: "C:\work\530aa31c-86c7-4712-a064-d9f00ce041fb\Figure_dfd8c611-8387-4579-804f-6384642ba4ff.fig"

失敗したテストに関連するイメージを検査するには、FullPath フィールドに示された場所にあるファイルを開きます。さらに、PDF のテスト レポートを生成しているため、イメージは MyTestReport.pdf に取得されています。テスト レポートには、アーティファクトへのパスも記載されています。

ヒント

  • Figure の保存場所は、ArtifactsRootFolder に含まれるフォルダー内で、テストの実行において固有の名前をもつフォルダーです。TestRunner を使用せずにテストを実行している場合 (matlab.unittest.TestCase.forInteractiveUse を使用する場合など)、ルート フォルダーは tempdir() で返される値です。

  • 保存した Figure のパスを判別するには、特定のテスト結果の FileArtifact オブジェクトにアクセスします。たとえば、resTestResult 配列であると仮定します。配列の最初の要素に関して保存した Figure の場所を、次のように判別します。

    res(1).Details.DiagnosticRecord.TestDiagnosticResults.Artifacts
    ans = 
    
      FileArtifact with properties:
    
            Name: "Figure_3984704d-b884-44c2-b3ee-7ed10d36e967.png"
        Location: "C:\mywork\Temp\a1f80242-8f8a-4678-9124-415980432d08"
        FullPath: "C:\mywork\Temp\a1f80242-8f8a-4678-9124-415980432d08\Figure_3984704d-b884-44c2-b3ee-7ed10d36e967.png"

バージョン履歴

R2017a で導入