Main Content

为 App 编写测试

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

在命令提示符下,通过将包含该 App 的文件夹添加到 MATLAB® 搜索路径,使该 App 可供访问。

addpath(fullfile(matlabroot,'examples','matlab','main'))

要在测试之前了解此 App 的属性,请在命令提示符下创建该 App 的实例。

app = PatientsDisplay;

此步骤对于测试不是必需的,但是了解 App 测试使用的属性会很有帮助。例如,使用 app.BloodPressureSwitch 访问 App 对象内的 Blood Pressure 开关。

创建一个从 matlab.uitest.TestCase 继承的测试类。要测试选项卡切换功能,请创建一个测试方法 test_tab。该测试方法选择 Data 选项卡,然后验证所选的选项卡是否具有正确的标题。TestMethodSetup 方法会为每个测试创建一个 App,并在测试完成后将其删除。

classdef TestPatientsDisplay < matlab.uitest.TestCase
    properties
        App
    end
    
    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;
            testCase.addTeardown(@delete,testCase.App);
        end
    end
    
    methods (Test)
        function test_tab(testCase)     
            % Choose Data Tab
            dataTab = testCase.App.DataTab;
            testCase.choose(dataTab)
            
            % Verify Data Tab is selected
            testCase.verifyEqual(testCase.App.TabGroup.SelectedTab.Title,'Data')
        end
        
    end
end

创建一个用于测试各种绘图选项的 test_plottingOptions 方法。该测试方法按下 Histogram 单选按钮并验证 x 标签已更改。然后,它会更改 Bin Width 滑块并验证 bin 的数量。

classdef TestPatientsDisplay < matlab.uitest.TestCase
    properties
        App
    end
    
    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;
            testCase.addTeardown(@delete,testCase.App);
        end
    end
    
    methods (Test)
        function test_plottingOptions(testCase)
            % Press the histogram radio button
            testCase.press(testCase.App.HistogramButton)
            
            % Verify xlabel updated 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 is now 4
            testCase.verifyEqual(testCase.App.UIAxes.Children.NumBins,4)
        end
        
        function test_tab(testCase) ...    
        
    end
end

创建一个用于测试血压数据和显示的 test_bloodPressure 方法。该测试方法验证 y 轴标签和散点的值。然后它切换到 Diastolic 读数,并再次验证标签和数据。

classdef TestPatientsDisplay < matlab.uitest.TestCase
    properties
        App
    end
    
    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;
            testCase.addTeardown(@delete,testCase.App);
        end
    end
    
    methods (Test)
        function test_bloodPressure(testCase)
            % Extract blood pressure data from app
            t = testCase.App.DataTab.Children.Data;
            t.Gender = categorical(t.Gender);
            allMales = t(t.Gender == 'Male',:);
            maleDiastolicData = allMales.Diastolic';
            maleSystolicData = allMales.Systolic';
            
            % Verify ylabel and that male Systolic data shows
            ax = testCase.App.UIAxes;
            testCase.verifyEqual(ax.YLabel.String,'Systolic')
            testCase.verifyEqual(ax.Children.YData,maleSystolicData)
            
            % Switch to 'Diastolic' reading
            testCase.choose(testCase.App.BloodPressureSwitch,'Diastolic')
            
            % Verify ylabel changed and male Diastolic data shows
            testCase.verifyEqual(ax.YLabel.String,'Diastolic')
            testCase.verifyEqual(ax.Children.YData,maleDiastolicData);
        end

        function test_plottingOptions(testCase) ...
        
        function test_tab(testCase) ...     
        
    end
end

创建一个用于测试性别数据和显示的 test_gender 方法。该测试方法验证男性散点的数量,然后启用相应复选框以包含女性数据。它验证绘制了两个数据集,并且女性数据的颜色为红色。最后,它清除男性数据复选框并验证绘图数据集和散点的数量。此测试失败,因为有 53 个女性散点而不是 50 个。要在测试失败时进行屏幕截图,请使用 ScreenshotDiagnostic 并配合 onFailure 方法。

classdef TestPatientsDisplay < matlab.uitest.TestCase
    properties
        App
    end
    
    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;
            testCase.addTeardown(@delete,testCase.App);
        end
    end
    
    methods (Test)
        function test_gender(testCase)
            import matlab.unittest.diagnostics.ScreenshotDiagnostic
            testCase.onFailure(ScreenshotDiagnostic);
            
            % Verify 47 male scatter points
            ax = testCase.App.UIAxes;
            testCase.verifyNumElements(ax.Children.XData,47);
            
            % Enable the checkbox for female data
            testCase.choose(testCase.App.FemaleCheckBox);
            
            % Verify two data sets display and the female data is red
            testCase.assertNumElements(ax.Children,2);
            testCase.verifyEqual(ax.Children(1).CData,[1 0 0]);
            
            % Disable the male data
            testCase.choose(testCase.App.MaleCheckBox,false);
            
            % Verify one data set displays and number of scatter points
            testCase.verifyNumElements(ax.Children,1);
            testCase.verifyNumElements(ax.Children.XData,50);
        end
        
        function test_bloodPressure(testCase)
            % Extract blood pressure data from app
            t = testCase.App.DataTab.Children.Data;
            t.Gender = categorical(t.Gender);
            allMales = t(t.Gender == 'Male',:);
            maleDiastolicData = allMales.Diastolic';
            maleSystolicData = allMales.Systolic';
            
            % Verify ylabel and that male Systolic data shows
            ax = testCase.App.UIAxes;
            testCase.verifyEqual(ax.YLabel.String,'Systolic')
            testCase.verifyEqual(ax.Children.YData,maleSystolicData)
            
            % Switch to 'Diastolic' reading
            testCase.choose(testCase.App.BloodPressureSwitch,'Diastolic')
            
            % Verify ylabel changed and male Diastolic data shows
            testCase.verifyEqual(ax.YLabel.String,'Diastolic')
            testCase.verifyEqual(ax.Children.YData,maleDiastolicData);
        end

        function test_plottingOptions(testCase)
            % Press the histogram radio button
            testCase.press(testCase.App.HistogramButton)
            
            % Verify xlabel updated 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 is now 4
            testCase.verifyEqual(testCase.App.UIAxes.Children.NumBins,4)
        end
        
        function test_tab(testCase)     
            % Choose Data Tab
            dataTab = testCase.App.DataTab;
            testCase.choose(dataTab)
            
            % Verify Data Tab is selected
            testCase.verifyEqual(testCase.App.TabGroup.SelectedTab.Title,'Data')
        end
        
    end
end

运行测试。

results = runtests('TestPatientsDisplay');
Running TestPatientsDisplay

================================================================================
Verification failed in TestPatientsDisplay/test_gender.
    ---------------------
    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 49
    
       131   133   119   142   142   132   128   137   129   131   133   117   137   146   123   143   114   126   137   138   137   118   128   135   121   136   135   147   124   134   130   130   127   141   111   134   137   136   130   137   127   127   115   131   126   120   132   120   123
    
      Columns 50 through 53
    
       141   129   124   134
    ----------------------
    Additional Diagnostic:
    ----------------------
    Screenshot captured to:
    --> C:\Temp\83292efd-b703-46ef-8c41-00e20167321d\Screenshot_c025020f-281e-483c-8ca8-f1c857421fde.png
    ------------------
    Stack Information:
    ------------------
    In C:\Work\TestPatientsDisplay.m (TestPatientsDisplay.test_gender) at 34
================================================================================
....
Done TestPatientsDisplay
__________

Failure Summary:

     Name                             Failed  Incomplete  Reason(s)
    ==============================================================================
     TestPatientsDisplay/test_gender    X                 Failed by verification.

另请参阅

相关主题