MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Add Plugin to Test Runner


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.

Create a Test for the BankAccount Class

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);

33 Unit Testing

Free download pdf