This example shows how to add a plugin to the test runner. The matlab.unittest.plugins.TestRunProgressPlugin
displays progress messages about a test case. This plugin is part of the matlab.unittest
package. MATLAB® uses it for default test runners.
In a file in your working folder, create a test file for the BankAccount
class.
type BankAccountTest.m
classdef BankAccountTest < matlab.unittest.TestCase % Tests the BankAccount class. methods (TestClassSetup) function addBankAccountClassToPath(testCase) p = path; testCase.addTeardown(@path,p); addpath(fullfile(matlabroot,'help','techdoc','matlab_oop',... 'examples')); end end 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 testDesposit(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
At the command prompt, create a test suite, ts
, from the BankAccountTest
test case.
ts = matlab.unittest.TestSuite.fromClass(?BankAccountTest);
Create a test runner with no plugins.
runner = matlab.unittest.TestRunner.withNoPlugins; res = runner.run(ts);
No output displayed.
Add the custom plugin, TestRunProgressPlugin
.
import matlab.unittest.plugins.TestRunProgressPlugin
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))
res = runner.run(ts);
Running BankAccountTest ..... Done BankAccountTest __________
MATLAB displays progress messages about BankAccountTest
.