向测试运行器添加插件
此示例演示如何向测试运行器添加插件。此示例添加一个使用 matlab.unittest.plugins.TestRunProgressPlugin
类创建的插件,该插件报告测试运行进度。
为 BankAccount
类创建测试
在当前文件夹内一个名为 BankAccount.m
的文件中创建 BankAccount
类。
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
在当前文件夹中另一个名为 BankAccountTest.m
的文件中,创建一个测试类来测试 BankAccount
类。
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
创建测试套件
基于 BankAccountTest
测试类创建一个测试套件。
suite = matlab.unittest.TestSuite.fromClass(?BankAccountTest);
运行没有插件的测试
创建一个没有插件的测试运行器,并使用它来运行测试。测试运行器以静默方式运行测试,并且不显示任何消息。
runner = matlab.unittest.TestRunner.withNoPlugins; runner.run(suite);
运行具有插件的测试
向测试运行器添加一个 TestRunProgressPlugin
实例并再次运行测试。现在,测试运行器显示关于 BankAccountTest
的测试运行进度。(默认测试运行器已包含此插件。)
import matlab.unittest.plugins.TestRunProgressPlugin
runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))
runner.run(suite);
Running BankAccountTest ..... Done BankAccountTest __________