Main Content

Write Test for App

This example shows how to write tests for an App Designer app. To interact with the app programmatically and qualify the results, use the app testing framework and the unit testing framework.

At the command prompt, make the app accessible by adding the folder that includes the app to the MATLAB® search path.

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

To explore the properties of this app prior to testing, create an instance of the app at the command prompt.

app = PatientsDisplay;

This step is not necessary for the tests, but it is helpful to explore the properties used by the app tests. For example, use app.BloodPressureSwitch to access the Blood Pressure switch within the app object.

Create a test class that inherits from matlab.uitest.TestCase. To test the tab switching functionality, create a test method test_tab. The test method chooses the Data tab and then verifies that the selected tab has the correct title. The TestMethodSetup method creates an app for each test and deletes it after the test is complete.

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

Create a test_plottingOptions method that tests various plotting options. The test method presses the Histogram radio button and verifies that the x-label changed. Then, it changes the Bin Width slider and verifies the number of bins.

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

Create a test_bloodPressure method that tests blood pressure data and display. The test method verifies the y-axis label and the values of the scatter points. Then it changes to Diastolic readings, and verifies the label and data again.

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

Create a test_gender method that tests gender data and display. The test method verifies the number of male scatter points and then presses the check box to include female data. It verifies that two data sets are plotted and the color of the female data is red. Finally, it clears the male data check box and verifies the number of plotted data sets and scatter points. This test fails because there are 53 female scatter points instead of 50. To take a screenshot when the test fails, use a ScreenshotDiagnostic with the onFailure method.

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

Run the tests.

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.

See Also

Related Topics