Analyze Test Case Results - MATLAB & Simulink (original) (raw)

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 r = quadraticSolver(a,b,c) % quadraticSolver returns solutions to the % quadratic equation ax^2 + bx + 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 - 4ac)) / (2a); r(2) = (-b - sqrt(b^2 - 4ac)) / (2a);

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 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 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.

Name Size Bytes Class Attributes

result 1x3 9164 matlab.unittest.TestResult

Display Information for One Test

To see the information for one value, type:

ans = TestResult with properties:

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

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

Create Table of Test Results

To access functionality available to tables, create one from the TestResult object.

rt=3×6 table Name Passed Failed Incomplete Duration Details
________________________________ ______ ______ __________ _________ ____________

{'SolverTest/realSolution'     }    true      false       false       0.0066456    {1×1 struct}
{'SolverTest/imaginarySolution'}    true      false       false       0.0067508    {1×1 struct}
{'SolverTest/nonnumericInput'  }    true      false       false        0.011253    {1×1 struct}

Sort the test results by duration.

ans=3×6 table Name Passed Failed Incomplete Duration Details
________________________________ ______ ______ __________ _________ ____________

{'SolverTest/realSolution'     }    true      false       false       0.0066456    {1×1 struct}
{'SolverTest/imaginarySolution'}    true      false       false       0.0067508    {1×1 struct}
{'SolverTest/nonnumericInput'  }    true      false       false        0.011253    {1×1 struct}

Export test results to a CSV file.

writetable(rt,'myTestResults.csv','QuoteStrings',true)

See Also

Topics