Documentation Center

  • Trial Software
  • Product Updates

Analyze Test Case Results

This example shows how to analyze the information returned by a test runner created from the SolverTest test case.

Create Quadratic Solver Function

Create the following function that solves roots of the quadratic equation in a file, quadraticSolver.m, in your working folder.

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 Test for Quadratic Solver Function

Create the following test class in a file, SolverTest.m, in your working folder.

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 SolverTest Test Case

Create a test suite, quadTests.

quadTests = matlab.unittest.TestSuite.fromClass(?SolverTest);
result = run(quadTests);
Running SolverTest
..
Done SolverTest
__________

All tests passed.

Explore Output Argument, result

The output argument, result, is a matlab.unittest.TestResult object. It contains information of the two tests in SolverTest.

whos result
  Name        Size            Bytes  Class

  result      1x2               248  matlab.unittest.TestResult  

Display Information for One Test

To see the information for one value, type:

result(1)
ans = 
  TestResult with properties:

          Name: 'SolverTest/testRealSolution'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0244
Totals:
   1 Passed, 0 Failed, 0 Incomplete.
   0.024365 seconds testing time.

Related Examples

Was this topic helpful?