testrunner
说明
runner = testrunner
创建一个默认测试运行器,它类似于测试框架在您调用 runtests
函数时默认配置的运行器。
testrunner
函数返回 matlab.unittest.TestRunner
对象。您可以对该返回的对象调用各种方法,以运行和操作您的测试套件,并自定义运行测试。例如,要运行一个测试套件,请使用 run(runner,suite)
。
runner = testrunner('minimal')
创建一个未安装插件的最小运行器。返回的测试运行器是尽可能最简单的运行器,且不会产生文本输出。当您需要完全控制要添加到运行器的插件时,请使用此语法。
runner = testrunner('textoutput')
创建一个为文本输出配置的运行器。所生成的输出包括测试进度以及测试失败时的诊断情况。
此语法可创建运行测试速度更快的运行器,因为测试框架不会记录由非默认运行器生成的测试结果的诊断信息。有关详细信息,请参阅以编程方式访问测试诊断。
示例
创建和使用默认测试运行器
使用默认运行器运行一个测试套件,并访问结果。
在当前文件夹中创建一个基于函数的测试 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. --> 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.
提示
使用
testrunner('minimal')
创建的运行器与使用matlab.unittest.TestRunner.withNoPlugins
方法创建的运行器相同。使用
testrunner('textoutput')
创建的运行器与使用不带输入参量的matlab.unittest.TestRunner.withTextOutput
方法创建的运行器相同。
版本历史记录
在 R2021a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)