MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
end

The following steps show how to create specific tests. Put these tests inside the methods
block with the (Test) attribute.

Create Test Method for Real Solutions

Create a test method, testRealSolution, to verify that quadraticSolver returns the
right value for real solutions. For example, the equation has real
solutions and. This method calls quadraticSolver with the inputs of this
equation. The solution, expSolution, is [2,1].

Use the matlab.unittest.TestCase method, verifyEqual to compare the output of
the function, actSolution, to the desired output, expSolution. If the qualification
fails, the test continues execution.

% Copyright 2015 The MathWorks, Inc.

function testRealSolution(testCase)
actSolution = quadraticSolver(1,-3,2);
expSolution = [2,1];
testCase.verifyEqual(actSolution,expSolution)
end

Add this function inside the methods (Test) block.

Create Test Method for Imaginary Solutions

Create a test to verify that quadraticSolver returns the right value for imaginary
solutions. For example, the equation has imaginary solutions
and. Add this function, testImaginarySolution, inside the
methods (Test) block.

% Copyright 2015 The MathWorks, Inc.

function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);

33 Unit Testing

Free download pdf