Main Content

Write Simple Test Case Using Classes

This example shows how to write unit tests for a MATLAB® function quadraticSolver.

Create quadraticSolver Function

In a file in your current folder, create the function quadraticSolver, which returns the roots of quadratic polynomials.

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

To test the quadraticSolver function, create a test class by subclassing the matlab.unittest.TestCase class. Add your tests inside a methods block with the Test attribute. The order of the tests within the block does not matter.

classdef SolverTest < matlab.unittest.TestCase

    methods (Test)

    end

end

Create Test Method for Real Solutions

Create a test method testRealSolution to verify that quadraticSolver returns the correct solutions. For example, the equation x2-3x+2=0 has real solutions x=1 and x=2. The method calls quadraticSolver with the coefficients of this equation. Then, it uses the verifyEqual method of matlab.unittest.TestCase to compare the output of the function, actSolution, to the desired output, expSolution. If the qualification fails, test execution continues.

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

Create Test Method for Imaginary Solutions

Create a test method testImaginarySolution to verify that quadraticSolver returns the correct imaginary solutions. For example, the equation x2-2x+10=0 has imaginary solutions x=-1+3i and x=-1-3i.

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

Create Test Method for Error Condition

Create a test method testNonNumericInput to verify that quadraticSolver produces an error for nonnumeric coefficients. Use the verifyError method of matlab.unittest.TestCase to test that the function throws the exception specified by the error identifier 'quadraticSolver:InputMustBeNumeric'.

function testNonNumericInput(testCase)
    testCase.verifyError(@()quadraticSolver(1,'-3',2), ...
        'quadraticSolver:InputMustBeNumeric')
end

SolverTest Class Definition Summary

This code provides the complete contents of the SolverTest class.

classdef SolverTest < matlab.unittest.TestCase
    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
        function testNonNumericInput(testCase)
            testCase.verifyError(@()quadraticSolver(1,'-3',2), ...
                'quadraticSolver:InputMustBeNumeric')
        end
    end
end

Run Tests in SolverTest Class

Run all of the tests in the SolverTest class.

testCase = SolverTest;
res = run(testCase)
Running SolverTest
...
Done SolverTest
__________
res = 
  1×3 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   3 Passed, 0 Failed, 0 Incomplete.
   1.4335 seconds testing time.

Run Single Test Method

Run the testRealSolution method.

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.0142
       Details: [1×1 struct]

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

Related Topics