Documentation Center

  • Trials
  • Product Updates

Add Plugin to Test Runner

This example shows how to add a plugin to the test runner. The matlab.unittest.plugins.TestSuiteProgressPlugin displays progress messages about a test case. This plugin is part of the matlab.unittest package. MATLAB® uses it for default test runners.

Create a Test for the BankAccount Class

In a file in your working folder, create a test file for the BankAccount class.

classdef BankAccountTest < matlab.unittest.TestCase
    % Tests the BankAccount class.
    
    methods (TestClassSetup)
        function addBankAccountClassToPath(testCase)
            testCase.addTeardown(@path, 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('BankAccount:InvalidInitialization'));
        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

Create Test Suite

At the command prompt, create a test suite, ts, from the BankAccountTest test case.

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

Show Results with No Plugins

Create a test runner with no plugins.

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

No output displayed.

Customize Test Runner

Add the custom plugin, TestSuiteProgressPlugin.

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

MATLAB displays progress messages about BankAccountTest.

See Also

Was this topic helpful?