Main Content

分析失败的测试结果

此示例演示如何识别和重新运行失败的测试。

创建不正确的测试方法

使用 SolverTest 测试用例,添加一个方法 testBadRealSolution。此测试(基于 testRealSolution)使用输入 1,3,2 调用 quadraticSolver,但根据不正确的解 [2,1] 测试结果。

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

运行新测试套件

保存更新的 SolverTest 类定义并重新运行测试。

quadTests = matlab.unittest.TestSuite.fromClass(?SolverTest);
result1 = run(quadTests);
Running SolverTest
..
================================================================================
Verification failed in SolverTest/testBadRealSolution.

    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyEqual failed.
    --> The values are not equal using "isequaln".
    --> Failure table:
                Index    Actual    Expected    Error    RelativeError
                _____    ______    ________    _____    _____________
            
                1        -1        2           -3       -1.5         
                2        -2        1           -3         -3         
    
    Actual Value:
            -1    -2
    Expected Value:
             2     1

    ------------------
    Stack Information:
    ------------------
    In C:\work\SolverTest.m (SolverTest.testBadRealSolution) at 19
================================================================================
.
Done SolverTest
__________

Failure Summary:

     Name                            Failed  Incomplete  Reason(s)
    =============================================================================
     SolverTest/testBadRealSolution    X                 Failed by verification.

分析结果

输出告知您 SolverTest/testBadRealSolution 失败。从 Framework Diagnostic,您可以看到以下内容:

    Actual Value:
            -1    -2
    Expected Value:
             2     1

此时,您必须决定错误是存在于 quadraticSolver 中还是 expSolution 的值中。

更正错误

testBadRealSolution 中编辑 expSolution 的值:

expSolution = [-1 -2];

重新运行测试

保存 SolverTest 并仅重新运行已失败的测试。

failedTests = quadTests([result1.Failed]);
result2 = run(failedTests)
Running SolverTest
.
Done SolverTest
__________


result2 = 

  TestResult with properties:

          Name: 'SolverTest/testBadRealSolution'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0108
       Details: [1x1 struct]

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

您也可以使用测试结果中的 (rerun) 链接重新运行失败的测试。

相关主题