主要内容

testrunner

创建测试运行器。

自 R2021a 起

说明

runner = testrunner 创建一个默认测试运行器,它等效于测试框架在您调用 runtests 函数时默认配置的运行器。

testrunner 函数返回 matlab.unittest.TestRunner 对象。您可以对该返回的对象调用各种方法,以运行和操作您的测试套件,并自定义运行测试。例如,要运行一个测试套件,请使用 run(runner,suite)

示例

runner = testrunner("minimal") 创建一个未安装插件的最小运行器。返回的测试运行器是尽可能最简单的运行器,且不会产生文本输出。当您需要完全控制要添加到运行器的插件时,请使用此语法。

示例

runner = testrunner("textoutput") 创建一个为文本输出配置的运行器。所生成的输出包括测试进度以及测试失败时的诊断情况。

此语法可创建运行测试速度更快的运行器,因为测试框架不会记录由非默认运行器生成的测试结果的诊断信息。有关详细信息,请参阅以编程方式访问测试诊断

示例

runner = testrunner(___,Name=Value) 支持上述语法中的任何输入参量组合,且可使用一个或多个名称-值参量指定选项。例如,runner = testrunner("textoutput",OutputDetail="Verbose") 创建一个测试运行器,它以最高详细级别显示测试输出详细信息。 (自 R2024b 起)

示例

全部折叠

使用默认运行器运行一个测试套件,并访问结果。

在当前文件夹中创建一个基于函数的测试 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');
runner = testrunner;
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.

显示第二个测试的结果。

results(2)
ans = 

  TestResult with properties:

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

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

当您使用默认运行器运行测试时,测试框架使用一个 DiagnosticsRecordingPlugin 实例来记录关于测试结果的诊断信息。使用 TestResult 对象的 Details 属性中的 DiagnosticRecord 字段访问第二次测试的所记录的诊断信息。

records = results(2).Details.DiagnosticRecord
records = 

  QualificationDiagnosticRecord with properties:

                          Event: 'VerificationFailed'
                     EventScope: TestMethod
                  EventLocation: 'sampleTest/testB'
          TestDiagnosticResults: [1×0 matlab.automation.diagnostics.DiagnosticResult]
     FrameworkDiagnosticResults: [1×1 matlab.automation.diagnostics.DiagnosticResult]
    AdditionalDiagnosticResults: [1×0 matlab.automation.diagnostics.DiagnosticResult]
                          Stack: [1×1 struct]
                         Report: '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'

通过创建一个最小运行器,然后向运行器添加一个 XMLPlugin 实例,生成 JUnit 样式的测试结果。

在当前文件夹中创建一个基于函数的测试 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');

创建一个不含任何插件的测试运行器。以下代码将创建一个不产生任何输出的静默运行器。您现在可以安装您喜欢的任何插件。

runner = testrunner('minimal');

创建一个 XMLPlugin 实例,它将 JUnit 样式的 XML 输出写入文件 myTestResults.xml

import matlab.unittest.plugins.XMLPlugin
xmlFile = 'myTestResults.xml';
p = XMLPlugin.producingJUnitFormat(xmlFile);

将该插件添加到测试运行器中并运行测试。

addPlugin(runner,p)
results = run(runner,suite);

显示第二个测试的结果。

results(2)
ans = 
  TestResult with properties:

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

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

检查记录的关于测试结果的诊断信息。如果您使用的是默认运行器,则此位置会有一个 DiagnosticRecord 字段。但是,由于您使用的是非默认运行器,框架不会创建这样的字段。

records = results(2).Details
records = struct with no fields.


现在,查看生成的工件的内容。

disp(fileread(xmlFile))
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<testsuites>

  <testsuite errors="0" failures="1" name="sampleTest" skipped="0" tests="3" time="0.083731">
    <testcase classname="sampleTest" name="testA" time="0.0085045"/>
    <testcase classname="sampleTest" name="testB" time="0.0723">
      <failure type="VerificationFailure">Verification failed in sampleTest/testB.
    ---------------------
    Framework Diagnostic:
    ---------------------
    verifyGreaterThan failed.
    --&gt; The value must be greater than the minimum value.
    
    Actual Value:
        13
    Minimum Value (Exclusive):
        42
    ------------------
    Stack Information:
    ------------------
    In C:\TEMP\Examples\matlab-ex97531283\sampleTest.m (testB) at 10</failure>
    </testcase>
    <testcase classname="sampleTest" name="testC" time="0.0029273"/>
  </testsuite>

</testsuites>

使用为文本输出配置的运行器运行一个测试套件,然后访问结果。

在当前文件夹中创建一个基于函数的测试 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');

创建一个生成文本输出的运行器,并使用它来运行测试。

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:\TEMP\Examples\matlab-ex48684143\sampleTest.m (testB) at 10
================================================================================
..
Done sampleTest
__________

Failure Summary:

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

显示第二个测试的结果。

results(2)
ans = 
  TestResult with properties:

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

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

检查记录的关于测试结果的诊断信息。如果您使用的是默认运行器,则此位置会有一个 DiagnosticRecord 字段。但是,由于您使用的是非默认运行器,框架不会创建这样的字段。

records = results(2).Details
records = struct with no fields.


名称-值参数

全部折叠

Name1=Value1,...,NameN=ValueN 的形式指定可选参量对组,其中 Name 是参量名称,Value 是对应的值。名称-值参量必须出现在其他参量之后,但对各个参量对组的顺序没有要求。

示例: runner = testrunner("textoutput",OutputDetail="Verbose",LoggingLevel="Detailed")

测试输出的显示级别,指定为从 04 的整数标量、matlab.automation.Verbosity 枚举对象或枚举的文本表示。

数值表示枚举成员名称详细程度描述
0None

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

默认情况下,测试运行器显示 matlab.automation.Verbosity.Detailed 级别(级别 3)的失败事件和记录的事件,以及 matlab.automation.Verbosity.Concise 级别(级别 2)的测试运行进度。

示例: runner = testrunner("textoutput",OutputDetail="Verbose") 创建一个在 matlab.automation.Verbosity.Verbose 级别显示测试输出的测试运行器。

已记录诊断的详细级别,指定为从 04 的整数标量、matlab.automation.Verbosity 枚举对象或枚举的文本表示。测试运行器包括在指定级别及以下级别记录的诊断。

数值表示枚举成员名称详细程度描述
0None

无信息

1Terse

最少的信息

2Concise

适中信息量

3Detailed

部分补充信息

4Verbose

大量补充信息

默认情况下,测试运行器包括在 matlab.automation.Verbosity.Terse 级别(级别 1)记录的诊断。要排除所记录的诊断信息,请将 LoggingLevel 指定为 matlab.automation.Verbosity.None(级别 0)。

记录的诊断是您使用 log (TestCase)log (Fixture) 方法为单元测试框架提供的诊断。

示例: runner = testrunner("textoutput",LoggingLevel="Detailed") 创建一个测试运行器,其中包括在 matlab.automation.Verbosity.Detailed 类别及以下级别记录的诊断信息。

提示

  • 您可以使用 testrunner 函数代替 matlab.unittest.TestRunner 类的静态方法。下表显示与 matlab.unittest.TestRunner 类的每个静态方法等效的 testrunner 函数调用。

    testrunner 函数matlab.unittest.TestRunner
    r = testrunnerr = matlab.unittest.TestRunner.withDefaultPlugins
    r = testrunner("minimal")r = matlab.unittest.TestRunner.withNoPlugins
    r = testrunner("textoutput")r = matlab.unittest.TestRunner.withTextOutput

版本历史记录

在 R2021a 中推出

全部展开