Main Content

アプリ用テストの記述

この例では、現在のフォルダー内の App Designer アプリ用テストを記述する方法を示します。プログラムによりアプリを操作して結果を検定するには、アプリ テスト フレームワークとユニット テスト フレームワークを使用します。

テスト前にこのアプリのプロパティを確認するには、アプリのインスタンスを作成します。この手順はテストには必要ありませんが、テストで使用されるプロパティを確認しておくと役に立ちます。たとえば、アプリ内の [Blood Pressure] スイッチにアクセスするために、app.BloodPressureSwitch を使用します。

app = PatientsDisplay;

現在のフォルダー内の PatientsDisplayTest.m という名前のファイルで、matlab.uitest.TestCase から派生させたテスト クラスを作成します。各テストのアプリを作成し、テストの完了後にそのアプリを削除するには、TestMethodSetup メソッドをクラスに追加します。次に、4 つの Test メソッドをクラスに追加します。

  • testTab メソッド — タブ切り替え機能をテストします。[データ] タブを選択してから、選択したタブのタイトルが期待どおりであることを確認します。

  • testPlottingOptions メソッド — さまざまなプロット オプションをテストします。まず、[ヒストグラム] ラジオ ボタンを押し、"x" 軸ラベルが変更されたことを確認します。次に、[ビンの幅] スライダーを変更し、ビンの数を検証します。

  • testBloodPressure メソッド — 血圧のデータと表示をテストします。まず、血圧のデータをアプリから抽出し、"y" 軸ラベルと、散布点の値を検証します。次に、Diastolic 測定値に切り替え、ラベルと表示値をもう一度検証します。

  • testGender メソッド — 性別のデータと表示をテストします。まず、男性についてのデータの散布点の数を検証します。次に、女性についてのデータを含め、2 つのデータ セットがプロットされていること、および女性についてのデータの散布点の色が赤であることを確認します。最後に、男性についてのデータを除外し、プロットされたデータ セットと散布点の数をテストします。

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

テストを実行します。この例では、3 つのテストにパスし、1 つのテストに失敗します。

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.

参考

|

関連するトピック