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

    methods (TestMethodSetup)
        function launchApp(testCase)
            testCase.App = PatientsDisplay;

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

            % Verify that the tab has the expected title
            testCase.verifyEqual( ...

        function testPlottingOptions(testCase)
            % Press the Histogram radio button

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

            % Change the bin width to 9

            % Verify the number of bins

        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;

            % Switch to Diastolic readings

            % Verify the y-axis label and that the male Diastolic data
            % is displayed

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

            % Verify the number of male scatter points
            ax = testCase.App.UIAxes;

            % Include the female data

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

            % Exclude the male data

            % Verify the number of displayed data sets and the number of
            % scatter points

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:
        Expected Number of Elements:
    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
    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