Write Simple Test Case Using Classes

This example shows how to write a unit test for a MATLAB® function, quadraticSolver.m.

Create quadraticSolver.m Function

The following MATLAB function solves quadratic equations. Create this function in a folder on your MATLAB path.

function roots = 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

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

end

Create SolverTest Class Definition

To use the matlab.unittest framework, write MATLAB functions (tests) in the form of a test case, a class derived from matlab.unittest.TestCase.

Create a subclass, SolverTest.


% Copyright 2015 The MathWorks, Inc.

classdef SolverTest < matlab.unittest.TestCase

    methods (Test)

    end

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 $x^{2} - 3x + 2 = 0$ has real solutions $x = 1$ and $x = 2$. 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 $x^{2} - 2x + 10 = 0$ has imaginary solutions $x = -1 + 3i$ and $x = -1 - 3i$. Add this function, testImaginarySolution, inside the methods (Test) block.


% Copyright 2015 The MathWorks, Inc.

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

The order of the tests within the block does not matter.

Save Class Definition

The following is the complete SolverTest class definition. Save this file in a folder on your MATLAB path.

classdef SolverTest < matlab.unittest.TestCase
    % SolverTest tests solutions to the quadratic equation
    % a*x^2 + b*x + c = 0
    
    methods (Test)
        function testRealSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2,1];
            testCase.verifyEqual(actSolution,expSolution);
        end
        function testImaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i, -1-3i];
            testCase.verifyEqual(actSolution,expSolution);
        end
    end
    
end 

Run Tests in SolverTest Test Case

Run all the tests in the SolverTest class definition file.

testCase = SolverTest;
res = run(testCase)
Running SolverTest
..
Done SolverTest
__________


res = 

  1x2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

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

Run Single Test Method

To run the single test, testRealSolution:

testCase = SolverTest;
res = run(testCase,'testRealSolution')
Running SolverTest
.
Done SolverTest
__________


res = 

  TestResult with properties:

          Name: 'SolverTest/testRealSolution'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0086
       Details: [1x1 struct]

Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.008623 seconds testing time.

Related Topics