Main Content

Write Simple Test Case Using Functions

You can test your MATLAB® program by defining unit tests within a single file that contains a main function and local test functions. In a function-based test, each local function executes a portion of the software and qualifies the correctness of the produced result. For more information about function-based tests, see Function-Based Unit Tests.

This example shows how to write a function-based test to qualify the correctness of a function defined in a file in your current folder. The quadraticSolver function takes as inputs the coefficients of a quadratic polynomial and returns the roots of that polynomial. If the coefficients are specified as nonnumeric values, the function throws an error.

function r = quadraticSolver(a,b,c)
% quadraticSolver returns solutions to the
% quadratic equation a*x^2 + b*x + c = 0.

if ~isa(a,'numeric') || ~isa(b,'numeric') || ~isa(c,'numeric')
    error('quadraticSolver:InputMustBeNumeric', ...
        'Coefficients must be numeric.');
end

r(1) = (-b + sqrt(b^2 - 4*a*c)) / (2*a);
r(2) = (-b - sqrt(b^2 - 4*a*c)) / (2*a);

end

Create Tests

To test the quadraticSolver function, create the test file quadraticSolverTest.m in your current folder. Then, define a main function and two local functions in the file to test quadraticSolver against real and imaginary solutions. The name of the main and local functions must start or end with the word "test", which is case-insensitive. Additionally, the name of the main function must correspond to the name of your test file.

Define Main Function

To run function-based unit tests, you must define a main function that collects all of the local test functions into a test array. Define the main function quadraticSolverTest in your test file. The main function calls functiontests to generate the test array tests. Pass localfunctions to functiontests to automatically generate a cell array of function handles to the local functions in your file.

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

Define Local Test Functions

Add local functions to the test file to test the quadraticSolver function against real and imaginary solutions. The order of the tests within the file does not matter. Each local function must accept a single input testCase, which is a matlab.unittest.FunctionTestCase object. The testing framework automatically generates this object. The function uses the object to perform qualifications for testing values and responding to failures.

Define a local function testRealSolution to verify that quadraticSolver returns the correct real solutions for specific coefficients. For example, the equation x2-3x+2=0 has real solutions x=1 and x=2. The function calls quadraticSolver with the coefficients of this equation. Then, it uses the verifyEqual qualification function to compare the actual output actSolution to the expected output expSolution.

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

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

Define a second local function testImaginarySolution to verify that quadraticSolver returns the correct imaginary solutions for specific coefficients. For example, the equation x2+2x+10=0 has imaginary solutions x=-1+3i and x=-1-3i. Just like the previous function, this function calls quadraticSolver with the coefficients of this equation, and then uses the verifyEqual qualification function to compare the actual output actSolution to the expected output expSolution.

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

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

function testImaginarySolution(testCase)
actSolution = quadraticSolver(1,2,10);
expSolution = [-1+3i -1-3i];
verifyEqual(testCase,actSolution,expSolution)
end

Run Tests in Test File

Use the runtests function to run the tests defined in the quadraticSolverTest.m file. In this example, both of the tests pass.

results = runtests('quadraticSolverTest.m')
Running quadraticSolverTest
..
Done quadraticSolverTest
__________
results = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   0.016572 seconds testing time.

You also can run tests using the run function.

results = run(quadraticSolverTest)
Running quadraticSolverTest
..
Done quadraticSolverTest
__________
results = 
  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   2 Passed, 0 Failed, 0 Incomplete.
   0.0072908 seconds testing time.

See Also

| |

Related Topics