MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Rerun Failed Tests


If a test failure is caused by incorrect or incomplete code, it is useful to rerun failed tests
quickly and conveniently. When you run a test suite, the test results include information
about the test suite and the test runner. If there are test failures in the results, when
MATLAB displays the test results there is a link to rerun the failed tests.

Totals:
1 Passed, 1 Failed (rerun), 0 Incomplete.
0.25382 seconds testing time.

This link allows you to modify your test code or your code under test and quickly rerun
failed tests. However, if you make structural changes to your test class, using the rerun
link does not pick up the changes. Structural changes include adding, deleting, or
renaming a test method, and modifying a test parameter property and its value. In this
case, recreate the entire test suite to pick up the changes.

Create the following function in your current working folder. The function is meant to
compute the square and square root. However, in this example, the function computes the
cube of the value instead of the square.

function [x,y] = exampleFunction(n)
validateattributes(n,{'numeric'},{'scalar'})

x = n^3; % square (incorrect code, should be n^2)
y = sqrt(n); % square root
end

Create the following test in a file exampleTest.m.

function tests = exampleTest
tests = functiontests(localfunctions);
end

function testSquare(testCase)
[sqrVal,sqrRootVal] = exampleFunction(3);
verifyEqual(testCase,sqrVal,9);
end

function testSquareRoot(testCase)
[sqrVal,sqrRootVal] = exampleFunction(100);
verifyEqual(testCase,sqrRootVal,10);
end

33 Unit Testing

Free download pdf