Main Content

このページの内容は最新ではありません。最新版の英語を参照するには、ここをクリックします。

テスト ランナーへのプラグインの追加

この例では、プラグインをテスト ランナーに追加する方法を説明します。matlab.unittest.plugins.TestRunProgressPlugin は、テスト ケースに関する進捗メッセージを表示します。このプラグインは、matlab.unittest パッケージの一部です。MATLAB® は、既定のテスト ランナーにこれを使用します。

BankAccount クラスのテストの作成

作業フォルダー内のファイルに、BankAccount クラスのファイルを作成します。

type BankAccount.m
classdef BankAccount < handle
   properties (Access = ?AccountManager)
      AccountStatus = 'open';
   end
   properties (SetAccess = private)
      AccountNumber
      AccountBalance
   end
   properties (Transient)
      AccountListener
   end
   events
      InsufficientFunds
   end
   methods
      function BA = BankAccount(accNum,initBal)
         BA.AccountNumber = accNum;
         BA.AccountBalance = initBal;
         BA.AccountListener =  AccountManager.addAccount(BA);
      end
      function deposit(BA,amt)
         BA.AccountBalance = BA.AccountBalance + amt;
         if BA.AccountBalance > 0
            BA.AccountStatus = 'open';
         end
      end
      function withdraw(BA,amt)
         if (strcmp(BA.AccountStatus,'closed')&& ...
               BA.AccountBalance < 0)
            disp(['Account ',num2str(BA.AccountNumber),...
               ' has been closed.'])
            return
         end
         newbal = BA.AccountBalance - amt;
         BA.AccountBalance = newbal;
         if newbal < 0
            notify(BA,'InsufficientFunds')
         end
      end
      function getStatement(BA)
         disp('-------------------------')
         disp(['Account: ',num2str(BA.AccountNumber)])
         ab = sprintf('%0.2f',BA.AccountBalance);
         disp(['CurrentBalance: ',ab])
         disp(['Account Status: ',BA.AccountStatus])
         disp('-------------------------')
      end
   end
   methods (Static)
      function obj = loadobj(s)
         if isstruct(s)
            accNum = s.AccountNumber;
            initBal = s.AccountBalance;
            obj = BankAccount(accNum,initBal);
         else
            obj.AccountListener = AccountManager.addAccount(s);
         end
      end
   end
end

BankAccount クラス用のテスト ファイルも作成します。

type BankAccountTest.m
classdef BankAccountTest < matlab.unittest.TestCase
    % Tests the BankAccount class.
    
    methods (Test)
        function testConstructor(testCase)
            b = BankAccount(1234, 100);
            testCase.verifyEqual(b.AccountNumber, 1234, ...
                'Constructor failed to correctly set account number');
            testCase.verifyEqual(b.AccountBalance, 100, ...
                'Constructor failed to correctly set account balance');
        end
        
        function testConstructorNotEnoughInputs(testCase)
            import matlab.unittest.constraints.Throws;
            testCase.verifyThat(@()BankAccount, ...
                Throws('MATLAB:minrhs'));
        end
        
        function testDeposit(testCase)
            b = BankAccount(1234, 100);
            b.deposit(25);
            testCase.verifyEqual(b.AccountBalance, 125);
        end
        
        function testWithdraw(testCase)
            b = BankAccount(1234, 100);
            b.withdraw(25);
            testCase.verifyEqual(b.AccountBalance, 75);
        end
        
        function testNotifyInsufficientFunds(testCase)
            callbackExecuted = false;
            function testCallback(~,~)
                callbackExecuted = true;
            end
            
            b = BankAccount(1234, 100);
            b.addlistener('InsufficientFunds', @testCallback);
            
            b.withdraw(50);
            testCase.assertFalse(callbackExecuted, ...
                'The callback should not have executed yet');
            b.withdraw(60);
            testCase.verifyTrue(callbackExecuted, ...
                'The listener callback should have fired');
        end
    end
end

テスト スイートの作成

コマンド プロンプトで、テストスイート tsBankAccountTest テスト ケースから作成します。

ts = matlab.unittest.TestSuite.fromClass(?BankAccountTest);

プラグインなしの結果の表示

プラグインなしでテスト ランナーを作成します。

runner = matlab.unittest.TestRunner.withNoPlugins;
res = runner.run(ts);

出力は表示されません。

テスト ランナーのカスタマイズ

カスタム プラグイン TestRunProgressPlugin を追加します。

import matlab.unittest.plugins.TestRunProgressPlugin
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))
res = runner.run(ts);
Running BankAccountTest
.....
Done BankAccountTest
__________

MATLAB には、BankAccountTest に関する進捗メッセージが表示されます。

参考