Main Content

Write Tests for an App

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

To explore the properties of the app prior to testing, create an instance of the app. This step is not necessary for the tests, but it is helpful to explore the properties used by the tests. For example, use app.BloodPressureSwitch to access the Blood Pressure switch within the app.

app = PatientsDisplay;

In a file named PatientsDisplayTest.m in your current folder, create a test class that derives from matlab.uitest.TestCase. To create an app for each test and delete it after the test, add a TestMethodSetup method to the class. Then, add four Test methods to the class:

  • testTab method — Test the tab-switching functionality. Choose the Data tab and then verify that the chosen tab has the expected title.

  • testPlottingOptions method — Test various plotting options. First, press the Histogram radio button and verify that the x-axis label changes. Then, change the Bin Width slider and verify the number of bins.

  • testBloodPressure method — Test the blood pressure data and display. First, extract the blood pressure data from the app, and verify the y-axis label and the values of the scatter points. Then, switch to Diastolic readings, and verify the label and the displayed values again.

  • testGender method — Test the gender data and display. First, verify the number of scatter points for data about males. Then, include the data about females, and verify that two data sets are plotted and that the color of the scatter points for data about females is red. Finally, exclude the data about males, and test the number of plotted data sets and scatter points.

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

Run the tests. In this example, three tests pass and one test fails.

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.

See Also

|

Related Topics