Main Content

为 App 编写测试

此示例说明如何在您当前文件夹中为通过 App 设计工具创建的 App 编写测试。要以编程方式与 App 交互并验证结果,请使用 App 测试框架和单元测试框架。

要在测试之前了解该 App 的属性,请创建该 App 的实例。此步骤对于测试不是必需的,但是了解测试使用的属性会很有帮助。例如,使用 app.BloodPressureSwitch 访问 App 对象内的血压开关。

app = PatientsDisplay;

在当前文件夹中名为 PatientsDisplayTest.m 的文件中,创建一个从 matlab.uitest.TestCase 派生的测试类。要为每个测试创建一个 App 并在测试后删除它,请向该类添加一个 TestMethodSetup 方法。然后,向该类添加四个 Test 方法:

  • testTab 方法 - 测试选项卡切换功能。选择数据选项卡,然后验证所选选项卡是否具有预期的标题。

  • testPlottingOptions 方法 - 测试各种绘图选项。首先,按下直方图单选按钮,并确认 x 轴标签会发生更改。然后,更改 bin 宽度滑块,并验证 bin 的数量。

  • testBloodPressure 方法 - 测试血压数据并显示。首先,从 App 中提取血压数据,并验证 y 轴标签和散点的值。然后,切换到 Diastolic 读数,并再次验证标签和显示的值。

  • testGender 方法 - 测试性别数据并显示。首先,验证男性数据的散点数量。然后,包含有关女性的数据,并验证绘制了两个数据集,而且有关女性的数据的散点颜色为红色。最后,排除关于男性的数据,并测试绘制的数据集和散点的数量。

classdef PatientsDisplayTest < matlab.uitest.TestCase
    properties
        App
    end

    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;
            testCase.addTeardown(@delete,testCase.App)
        end
    end

    methods (Test)
        function testTab(testCase)
            % Choose the Data tab
            dataTab = testCase.App.DataTab;
            testCase.choose(dataTab)

            % Verify that the tab has the expected title
            testCase.verifyEqual( ...
                testCase.App.TabGroup.SelectedTab.Title,'Data')
        end

        function testPlottingOptions(testCase)
            % Press the Histogram radio button
            testCase.press(testCase.App.HistogramButton)

            % Verify that the x-axis label changed from Weight to Systolic
            testCase.verifyEqual(testCase.App.UIAxes.XLabel.String, ...
                'Systolic')

            % Change the bin width to 9
            testCase.choose(testCase.App.BinWidthSlider,9)

            % Verify the number of bins
            testCase.verifyEqual(testCase.App.UIAxes.Children.NumBins,4)
        end

        function testBloodPressure(testCase)
            % Extract the blood pressure data from the app
            t = testCase.App.DataTab.Children.Data;
            t.Gender = categorical(t.Gender);
            allMales = t(t.Gender == "Male",:);
            maleDiastolicData = allMales.Diastolic';
            maleSystolicData = allMales.Systolic';

            % Verify the y-axis label and that the male Systolic data is
            % displayed
            ax = testCase.App.UIAxes;
            testCase.verifyEqual(ax.YLabel.String,'Systolic')
            testCase.verifyEqual(ax.Children.YData,maleSystolicData)

            % Switch to Diastolic readings
            testCase.choose(testCase.App.BloodPressureSwitch,'Diastolic')

            % Verify the y-axis label and that the male Diastolic data
            % is displayed
            testCase.verifyEqual(ax.YLabel.String,'Diastolic')
            testCase.verifyEqual(ax.Children.YData,maleDiastolicData)
        end

        function testGender(testCase)
            % Take a screenshot if the test fails
            import matlab.unittest.diagnostics.ScreenshotDiagnostic
            testCase.onFailure(ScreenshotDiagnostic)

            % Verify the number of male scatter points
            ax = testCase.App.UIAxes;
            testCase.verifyNumElements(ax.Children.XData,47)

            % Include the female data
            testCase.choose(testCase.App.FemaleCheckBox)

            % Verify the number of displayed data sets and the color
            % representing the female data
            testCase.assertNumElements(ax.Children,2)
            testCase.verifyEqual(ax.Children(1).CData,[1 0 0])

            % Exclude the male data
            testCase.choose(testCase.App.MaleCheckBox,false)

            % Verify the number of displayed data sets and the number of
            % scatter points
            testCase.verifyNumElements(ax.Children,1)
            testCase.verifyNumElements(ax.Children.XData,50)
        end
    end
end

运行测试。在此示例中,三个测试通过,一个测试失败。

results = runtests("PatientsDisplayTest");
Running PatientsDisplayTest
...
================================================================================
Verification failed in PatientsDisplayTest/testGender.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyNumElements failed.
    --> The value did not have the correct number of elements.
        
        Actual Number of Elements:
            53
        Expected Number of Elements:
            50
    
    Actual Value:
      Columns 1 through 13
    
       131   133   119   142   142   132   128   137   129   131   133   117   137
    
      Columns 14 through 26
    
       146   123   143   114   126   137   138   137   118   128   135   121   136
    
      Columns 27 through 39
    
       135   147   124   134   130   130   127   141   111   134   137   136   130
    
      Columns 40 through 52
    
       137   127   127   115   131   126   120   132   120   123   141   129   124
    
      Column 53
    
       134
    ----------------------
    Additional Diagnostic:
    ----------------------
    Screenshot captured to:
    --> C:\Temp\b5238869-2e26-4f74-838f-83b1929c4eb1\Screenshot_ad84e34f-7587-41ca-8a97-25c484bbcd70.png
    ------------------
    Stack Information:
    ------------------
    In C:\work\PatientsDisplayTest.m (PatientsDisplayTest.testGender) at 85
================================================================================
.
Done PatientsDisplayTest
__________

Failure Summary:

     Name                            Failed  Incomplete  Reason(s)
    =============================================================================
     PatientsDisplayTest/testGender    X                 Failed by verification.

另请参阅

|

相关主题