MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

'(x^2 + 1) + (5x + 2) = x^2 + 5x + 3'];
testCase.verifyEqual(actual, expected, msg)
end


function testMultiplication(testCase)
p1 = DocPolynom([1, 0, 3]);
p2 = DocPolynom([5, 2]);


actual = p1 * p2;
expected = DocPolynom([5, 2, 15, 6]);


msg = [testCase.msgEqn,...
'(x^2 + 3) (5x + 2) = 5x^3 + 2x^2 + 15*x + 6'];
testCase.verifyEqual(actual, expected, msg)
end


end
end


Create a Test for the BankAccount Class


Create a test file for the BankAccount class. Create the shared fixture by specifying the
SharedTestFixtures attribute for the TestCase and passing in a PathFixture.


BankAccountTest Class Definition File


classdef (SharedTestFixtures={matlab.unittest.fixtures.PathFixture( ...
fullfile(matlabroot, 'help', 'techdoc', 'matlab_oop', ...
'examples'))}) BankAccountTest < matlab.unittest.TestCase
% Tests the BankAccount class.


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


Write Tests Using Shared Fixtures
Free download pdf