MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
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

Build the Test Suite

The classes DocPolynomTest.m and BankAccountTest.m are in your working
directory. Create a test suite from your current working directory. If you have additional
tests, they are included in the suite when you use the TestSuite.fromFolder method.
Create the test suite at the command prompt.

import matlab.unittest.TestSuite;
suiteFolder = TestSuite.fromFolder(pwd);

Run the Tests

At the command prompt, run the tests in the test suite.

33 Unit Testing

Free download pdf