Main Content

run

类: matlab.unittest.TestRunner
命名空间: matlab.unittest

运行测试套件

说明

示例

results = run(runner,suite) 使用指定的测试运行器运行 TestSuite 数组 suite 并返回测试运行的结果。当运行测试套件时,该方法将根据测试的需要自动执行任意设置和拆解代码。

输入参数

全部展开

测试运行器,指定为 matlab.unittest.TestRunner 对象。

测试套件,指定为 matlab.unittest.TestSuite 数组。

输出参数

全部展开

运行测试套件的结果,以 matlab.unittest.TestResult 数组形式返回。results 的元素对应于 suite 的元素。

示例

全部展开

使用为文本输出配置的测试运行器运行一组测试。

在当前文件夹中创建一个基于函数的测试 sampleTest.m

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

function testA(testCase)      % Test passes
verifyEqual(testCase,2+3,5)
end

function testB(testCase)      % Test fails
verifyGreaterThan(testCase,13,42)
end

function testC(testCase)      % Test passes
verifySubstring(testCase,"Hello world!","llo")
end

基于 sampleTest.m 中的测试创建一个测试套件。

suite = testsuite("sampleTest.m");

创建一个生成文本输出的测试运行器,并使用它来运行测试。文本输出包括测试运行进度以及在测试失败时的诊断。

runner = testrunner("textoutput");
results = run(runner,suite);
Running sampleTest
.
================================================================================
Verification failed in sampleTest/testB.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyGreaterThan failed.
    --> The value must be greater than the minimum value.
    
    Actual Value:
        13
    Minimum Value (Exclusive):
        42
    ------------------
    Stack Information:
    ------------------
    In C:\work\sampleTest.m (testB) at 10
================================================================================
..
Done sampleTest
__________

Failure Summary:

     Name              Failed  Incomplete  Reason(s)
    ===============================================================
     sampleTest/testB    X                 Failed by verification.

显示失败测试的结果。

disp(results([results.Failed]))
  TestResult with properties:

          Name: 'sampleTest/testB'
        Passed: 0
        Failed: 1
    Incomplete: 0
      Duration: 1.2982
       Details: [1×1 struct]

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

版本历史记录

在 R2013a 中推出