Main Content

이 번역 페이지는 최신 내용을 담고 있지 않습니다. 최신 내용을 영문으로 보려면 여기를 클릭하십시오.

테스트 실행기(Test Runner)에 플러그인 추가하기

이 예제에서는 테스트 실행기에 플러그인을 추가하는 방법을 보여줍니다. 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

테스트 스위트 생성하기

명령 프롬프트에서, BankAccountTest 테스트 케이스로부터 테스트 스위트 ts를 만듭니다.

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에 대한 진행 상황 메시지를 표시합니다.

참고 항목