このページの翻訳は最新ではありません。ここをクリックして、英語の最新版を参照してください。
アプリ用のテストの記述
この例では、App Designer のアプリ用のテストを記述する方法を説明します。プログラムによりアプリを操作して結果を検定するには、アプリ テスト フレームワークとユニット テスト フレームワークを使用します。
コマンド プロンプトで、アプリを含むフォルダーを MATLAB® 検索パスに追加して、アプリにアクセスできるようにします。
addpath(fullfile(matlabroot,'examples','matlab','main'))
テストの前にこのアプリのプロパティを確認するため、コマンド プロンプトでアプリのインスタンスを作成します。
app = PatientsDisplay;
この手順はテストには必要ありませんが、アプリ テストで使用されるプロパティを確認しておくと役に立ちます。たとえば、アプリ オブジェクト内の [Blood Pressure] スイッチにアクセスするには app.BloodPressureSwitch
を使用します。
matlab.uitest.TestCase
から継承するテスト クラスを作成します。タブのスイッチ機能をテストするため、テスト メソッド test_tab
を作成します。このテスト メソッドは [Data] タブを選択して、選択したタブのタイトルが正しいことを検証します。TestMethodSetup
メソッドは、各テスト用にアプリを作成し、テストが完了した後でそれを削除します。
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] スライダーを変更し、ビンの数を検証します。
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
メソッドを作成します。このテスト メソッドは男性の散布点の数を検証した後、女性のデータを含めるチェック ボックスをクリックします。2 つのデータセットがプロットされ、女性のデータの色が赤になることを検証します。最後に、男性データのチェック ボックスをオフにして、プロットされるデータセットと散布点の数を検証します。女性の散布点が 50 個ではなく 53 個あるため、このテストは失敗します。テストが失敗したときにスクリーンショットを撮るには、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.