Add Plugin to Test Runner
This example shows how to add a plugin to the test runner. The example adds a plugin created using the matlab.unittest.plugins.TestRunProgressPlugin class, which reports on test run progress.
Create Tests for BankAccount Class
In a file named BankAccount.m in your current folder, create the BankAccount class.
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
In another file named BankAccountTest.m in your current folder, create a test class to test the BankAccount class.
classdef BankAccountTest < matlab.unittest.TestCase methods (Test) function testConstructor(testCase) b = BankAccount(1234,100); testCase.verifyEqual(b.AccountNumber,1234, ... "Constructor must correctly set account number.") testCase.verifyEqual(b.AccountBalance,100, ... "Constructor must 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
Create Test Suite
Create a test suite from the BankAccountTest test class.
suite = matlab.unittest.TestSuite.fromClass(?BankAccountTest);
Run Tests with No Plugins
Create a test runner with no plugins, and use it to run the tests. The test runner runs the tests silently and does not display any messages.
runner = matlab.unittest.TestRunner.withNoPlugins; runner.run(suite);
Run Tests with Plugin
Add a TestRunProgressPlugin instance to the test runner, and run the tests again. The test runner now displays test run progress about BankAccountTest. (Default test runners already include this plugin.)
import matlab.unittest.plugins.TestRunProgressPlugin
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))
runner.run(suite);Running BankAccountTest ..... Done BankAccountTest __________