Main Content

Write Simple Test Case Using Classes

You can test your MATLAB® program by defining unit tests within a test class that inherits from the matlab.unittest.TestCase class. A unit test in a class-based test is a method that determines the correctness of a unit of software. It is defined within a methods block with the Test attribute and can use qualifications for testing values and responding to failures. For more information about class-based tests, see Author Class-Based Unit Tests in MATLAB.

This example shows how to write class-based unit tests 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 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

In a file in your current folder, create the SolverTest class by subclassing the matlab.unittest.TestCase class. This class provides a place for tests for the quadraticSolver function. Add three unit tests inside a methods block with the Test attribute. These test the quadraticSolver function against real solutions, imaginary solutions, and error conditions. Each Test method must accept a TestCase instance as an input. The order of the tests within the block does not matter.

First, create a Test method realSolution 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 method calls quadraticSolver with the coefficients of this equation. Then, it uses the verifyEqual method of matlab.unittest.TestCase to compare the actual output actSolution to the expected output expSolution.

classdef SolverTest < matlab.unittest.TestCase
    methods(Test)
        function realSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2 1];
            testCase.verifyEqual(actSolution,expSolution)
        end
    end
end

Create a second Test method imaginarySolution 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 method, this method calls quadraticSolver with the coefficients of this equation, and then uses the verifyEqual method to compare the actual output actSolution to the expected output expSolution.

classdef SolverTest < matlab.unittest.TestCase
    methods(Test)
        function realSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2 1];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function imaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i -1-3i];
            testCase.verifyEqual(actSolution,expSolution)
        end
    end
end

Finally, add a Test method nonnumericInput 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 error specified by 'quadraticSolver:InputMustBeNumeric' when it is called with inputs 1, '-3', and 2.

classdef SolverTest < matlab.unittest.TestCase
    methods(Test)
        function realSolution(testCase)
            actSolution = quadraticSolver(1,-3,2);
            expSolution = [2 1];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function imaginarySolution(testCase)
            actSolution = quadraticSolver(1,2,10);
            expSolution = [-1+3i -1-3i];
            testCase.verifyEqual(actSolution,expSolution)
        end
        function nonnumericInput(testCase)
            testCase.verifyError(@()quadraticSolver(1,'-3',2), ...
                'quadraticSolver:InputMustBeNumeric')
        end
    end
end

Run Tests in SolverTest Class

To run all of the tests in the SolverTest class, create a TestCase object from the class and then call the run method on the object. In this example, all three tests pass.

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

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

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

You also can run a single test specified by one of the Test methods. To run a specific Test method, pass the name of the method to run. For example, run the realSolution method.

result = run(testCase,'realSolution')
Running SolverTest
.
Done SolverTest
__________
result = 
  TestResult with properties:

          Name: 'SolverTest/realSolution'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0082
       Details: [1×1 struct]

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

See Also

Related Topics