Main Content

重新运行失败的测试

如果测试失败是由不正确或不完整的代码引起的,则快速、方便地重新运行失败的测试很有用。当您运行一个测试套件时,测试结果包含有关该测试套件和测试运行器的信息。如果结果中有测试失败,则 MATLAB 显示的测试结果中会有用于重新运行失败测试的链接。

Totals:
   1 Passed, 1 Failed (rerun), 0 Incomplete.
   0.25382 seconds testing time.

通过此链接,您可以修改测试代码或被测代码,并快速重新运行失败的测试。但是,如果您对测试类进行了结构性更改,则使用重新运行链接不会反映所做更改。结构性更改包括添加、删除或重命名测试方法,以及修改测试参数属性及其值。在这种情况下,请重新创建整个测试套件以反映更改。

在您的当前工作文件夹中创建以下函数。该函数旨在计算平方和平方根。但是,在此示例中,函数计算的是值的立方而不是平方。

function [x,y] = exampleFunction(n)
    validateattributes(n,{'numeric'},{'scalar'})
    
    x = n^3;     % square (incorrect code, should be n^2)
    y = sqrt(n); % square root
end

在文件 exampleTest.m 中创建以下测试。

function tests = exampleTest
    tests = functiontests(localfunctions);
end

function testSquare(testCase)
    [sqrVal,sqrRootVal] = exampleFunction(3);
    verifyEqual(testCase,sqrVal,9);
end

function testSquareRoot(testCase)
    [sqrVal,sqrRootVal] = exampleFunction(100);
    verifyEqual(testCase,sqrRootVal,10);
end

创建一个测试套件并运行测试。testSquare 测试失败,因为 exampleFunction 的实现不正确。

suite = testsuite('ExampleTest.m');
results = run(suite)
Running exampleTest

================================================================================
Verification failed in exampleTest/testSquare.

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

    ------------------
    Stack Information:
    ------------------
    In C:\Work\exampleTest.m (testSquare) at 7
================================================================================
..
Done exampleTest
__________

Failure Summary:

     Name                    Failed  Incomplete  Reason(s)
    =====================================================================
     exampleTest/testSquare    X                 Failed by verification.
    

results = 

  1×2 TestResult array with properties:

    Name
    Passed
    Failed
    Incomplete
    Duration
    Details

Totals:
   1 Passed, 1 Failed (rerun), 0 Incomplete.
   0.24851 seconds testing time.

更新 exampleFunction 中的代码以修复代码编写错误。

function [x,y] = exampleFunction(n)
    validateattributes(n,{'numeric'},{'scalar'})
    
    x = n^2;     % square
    y = sqrt(n); % square root
end

点击命令行窗口中的 (rerun) 链接以重新运行失败的测试。如果存储测试结果的变量被覆盖,则您不能重新运行失败的测试。如果链接不再位于命令行窗口中,您可以在提示符下键入 results 以查看该链接。

Running exampleTest
.
Done exampleTest
__________


ans = 

  TestResult with properties:

          Name: 'exampleTest/testSquare'
        Passed: 1
        Failed: 0
    Incomplete: 0
      Duration: 0.0034
       Details: [1×1 struct]

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

MATLAB 将与您重新运行的测试相关联的 TestResult 数组存储在 ans 变量中。results 是 1×2 数组,其中包含 exampleTest.m 中的所有测试;ans 是 1×1 数组,其中包含来自一个失败测试的重新运行结果。

whos
   Name         Size            Bytes  Class                         Attributes

  ans          1x1               664  matlab.unittest.TestResult              
  results      1x2              1344  matlab.unittest.TestResult              
  suite        1x2                96  matlab.unittest.Test      

要以编程方式重新运行失败的测试,请对 TestResult 对象使用 Failed 属性以创建并运行一个已过滤测试套件。

failedTests = suite([results.Failed]);
result2 = run(failedTests);
Running exampleTest
.
Done exampleTest
__________

为确保所有通过的测试继续通过,请重新运行完整测试套件。

相关主题