分析测试用例结果

创建二次求解器函数

`type quadraticSolver.m`
```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 ```

为二次求解器函数创建测试

`type SolverTest.m`
```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 ```

运行 `SolverTest` 测试用例

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

浏览输出参数 ` result`

`whos result`
``` Name Size Bytes Class Attributes result 1x3 7613 matlab.unittest.TestResult ```

显示一个测试的信息

`result(1)`
```ans = TestResult with properties: Name: 'SolverTest/realSolution' Passed: 1 Failed: 0 Incomplete: 0 Duration: 0.0065 Details: [1×1 struct] Totals: 1 Passed, 0 Failed, 0 Incomplete. 0.0065241 seconds testing time. ```

创建测试结果表格

`rt = table(result)`
```rt=3×6 table Name Passed Failed Incomplete Duration Details ________________________________ ______ ______ __________ _________ ____________ {'SolverTest/realSolution' } true false false 0.0065241 {1×1 struct} {'SolverTest/imaginarySolution'} true false false 0.0036673 {1×1 struct} {'SolverTest/nonnumericInput' } true false false 0.0074686 {1×1 struct} ```

`sortrows(rt,'Duration')`
```ans=3×6 table Name Passed Failed Incomplete Duration Details ________________________________ ______ ______ __________ _________ ____________ {'SolverTest/imaginarySolution'} true false false 0.0036673 {1×1 struct} {'SolverTest/realSolution' } true false false 0.0065241 {1×1 struct} {'SolverTest/nonnumericInput' } true false false 0.0074686 {1×1 struct} ```

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