Main Content

matlab.unittest.diagnostics.FigureDiagnostic 类

命名空间: matlab.unittest.diagnostics

可保存指定图窗的诊断

描述

使用 FigureDiagnostic 类创建一个可将图窗保存为文件的诊断。在 MATLAB® 完成测试运行后,该文件依然存在,因此可用于测试后检查。

构造

FigureDiagnostic(fig) 创建可保存指定图窗的诊断。当测试框架诊断 FigureDiagnostic 实例时,它会将 fig 保存为 FIG 文件和 PNG 文件。每个文件都具有唯一的名称,由前缀(默认为 'Figure_')、自动生成的标识符和文件扩展名组成。例如文件名 Figure_cf95fe7f-5a7c-4310-9c19-16c0c17a969f.png。要查看文件的位置,请通过 TestResult 实例访问 FileArtifact 对象。

FigureDiagnostic(fig,Name,Value) 创建具有额外选项的诊断,这些选项由一个或多个 Name,Value 对组参量指定。您可采用任意顺序指定多个名称-值对组参量,例如 Name1,Value1,...,NameN,ValueN。例如,FigureDiagnostic(fig,'Prefix','LoggedFigure_','Formats','png') 仅将 fig 保存为 PNG 文件,并使用前缀 'LoggedFigure_',而不是 'Figure_'

输入参量

全部展开

测试框架诊断 FigureDiagnostic 实例时保存的图窗,指定为使用 figureuifigure 函数创建的 Figure 对象。

名称-值参数

将可选的参量对组指定为 Name1=Value1,...,NameN=ValueN,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但参量对组的顺序无关紧要。

在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name 引起来。

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

图窗的文件格式,指定为 ["fig" "png"]"fig""png"。您可以将这些值指定为字符向量,例如 {'fig','png'}'fig''png'。在对象内,MATLAB 会将它们存储为字符串。

文件名前缀,指定为文本。如果不指定前缀,则默认前缀为 'Figure_'。可将该值指定为字符向量或字符串标量。在对象内,MATLAB 会将其存储为字符向量。

示例: 'LoggedFigure_'

示例: "TestFig-"

属性

全部展开

测试框架诊断 FigureDiagnostic 实例时保存的图窗,以 Figure 对象形式返回。Figure 属性是只读属性,其值是在构造期间设置的。

所保存图窗的文件格式,以 ["fig" "png"]"fig""png" 形式返回。Formats 属性是只读属性,其值是在构造期间设置的。

文件名前缀,以字符向量形式返回。默认前缀为 'Figure_'Prefix 属性是只读属性,其值是在构造期间设置的。

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

创建供交互使用的 TestCase

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

创建一个图窗。

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

使用 FigureDiagnostic 将该图窗保存为测试诊断。验证该图窗没有任何子级。此鉴定失败,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;

创建一个图窗。

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

使用 FigureDiagnostic 将该图窗记录为测试诊断。只将该文件保存为 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 保存图窗,以便于您以后检查。

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;

运行测试。第三个测试失败。

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.

显示第三个测试的测试诊断结果。测试框架保存了与第三个测试相关的两个工件。默认情况下,FigureDiagnostic 对象将图窗同时保存为 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 中也会包含捕获的图像。测试报告还包含工件路径。

提示

  • 保存图窗的位置是一个文件夹,其名称为对应于某次测试运行的唯一名称,位于 ArtifactsRootFolder 包含的文件夹中。如果您运行的测试不带 TestRunner,例如 matlab.unittest.TestCase.forInteractiveUse,则根文件夹是 tempdir() 返回的值。

  • 要确定保存图窗的路径,请访问具体测试结果的 FileArtifact 对象。例如,假定 res 是一个 TestResult 数组。按如下所示确定该数组第一个元素的图窗保存位置。

    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 中推出