addPlugin
类: matlab.unittest.TestRunner
命名空间: matlab.unittest
将插件添加到测试运行器
输入参数
runner
— 测试运行器
matlab.unittest.TestRunner
对象
测试运行器,指定为 matlab.unittest.TestRunner
对象。
plugin
— 插件
matlab.unittest.plugins.TestRunnerPlugin
对象
插件,指定为 matlab.unittest.plugins.TestRunnerPlugin
对象。
示例
生成测试工件
通过向测试运行器添加 XMLPlugin
实例来生成 JUnit 样式的测试结果。
在当前文件夹中名为 eyeTest.m
的文件中,创建一个基于函数的测试来测试 eye
函数。
function tests = eyeTest tests = functiontests(localfunctions); end function doubleClassTest(testCase) actual = eye; verifyClass(testCase,actual,"double") end function singleClassTest(testCase) actual = eye("single"); verifyClass(testCase,actual,"single") end function uint16ClassTest(testCase) actual = eye("uint16"); verifyClass(testCase,actual,"uint16") end function sizeTest(testCase) expected = [7 13]; actual = eye(expected); verifySize(testCase,actual,expected) end function valueTest(testCase) actual = eye(42); verifyEqual(testCase,unique(diag(actual)),1) % Diagonal values must be 1 verifyEqual(testCase,unique(triu(actual,1)),0) % Upper triangular values must be 0 verifyEqual(testCase,unique(tril(actual,-1)),0) % Lower triangular values must be 0 end
要运行测试,首先导入此示例中使用的类。
import matlab.unittest.TestRunner import matlab.unittest.plugins.XMLPlugin
基于 eyeTest.m
中的测试创建一个测试套件。
suite = testsuite("eyeTest.m");
创建一个不含任何插件的测试运行器。以下代码将创建一个不产生任何输出的静默运行器。
runner = matlab.unittest.TestRunner.withNoPlugins;
您现在可以添加任何您选择的插件。创建一个插件,它将 JUnit 样式的 XML 输出写入当前文件夹中的文件 myTestResults.xml
。然后,将该插件添加到测试运行器中。
xmlFile = "myTestResults.xml";
p = XMLPlugin.producingJUnitFormat(xmlFile);
addPlugin(runner,p)
运行测试。在此示例中,所有测试都通过。
results = run(runner,suite);
现在,查看生成的工件的内容。
disp(fileread(xmlFile))
<?xml version="1.0" encoding="UTF-8" standalone="no" ?> <testsuites> <testsuite errors="0" failures="0" name="eyeTest" skipped="0" tests="5" time="0.25082"> <testcase classname="eyeTest" name="doubleClassTest" time="0.015066"/> <testcase classname="eyeTest" name="singleClassTest" time="0.0042728"/> <testcase classname="eyeTest" name="uint16ClassTest" time="0.0046594"/> <testcase classname="eyeTest" name="sizeTest" time="0.013599"/> <testcase classname="eyeTest" name="valueTest" time="0.21322"/> </testsuite> </testsuites>
显示正在运行的测试的名称
使用插件运行测试,该插件将正在运行的测试的名称定向到输出流。将 ToStandardOutput
实例传递给插件,以便它将生成的文本定向到屏幕上。
创建自定义插件
在当前文件夹中名为 ExamplePlugin.m
的文件中,创建 ExamplePlugin
类,该类覆盖 TestRunnerPlugin
的 runTest
方法。该插件将正在运行的每个测试的名称定向至插件构造期间指定的输出流。
classdef ExamplePlugin < matlab.unittest.plugins.TestRunnerPlugin properties (SetAccess=immutable) Output end methods function plugin = ExamplePlugin(stream) arguments stream (1,1) matlab.automation.streams.OutputStream end plugin.Output = stream; end end methods (Access=protected) function runTest(plugin,pluginData) print(plugin.Output,"### Running test: %s\n",pluginData.Name) % Invoke the superclass method runTest@matlab.unittest.plugins.TestRunnerPlugin( ... plugin,pluginData) end end end
创建示例测试类
在当前文件夹中名为 ZerosTest.m
的文件中,创建 ZerosTest
类来测试 zeros
函数。
classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end methods (Test) function testClass(testCase,size,type) testCase.verifyClass(zeros(size,type),type) end function testSize(testCase,size) testCase.verifySize(zeros(size),size) end function testDefaultClass(testCase) testCase.verifyClass(zeros,"double") end function testDefaultSize(testCase) testCase.verifySize(zeros,[1 1]) end function testDefaultValue(testCase) testCase.verifyEqual(zeros,0) end end end
向测试运行器添加插件并运行测试
要运行测试,首先导入此示例中使用的类。
import matlab.unittest.TestRunner import matlab.automation.streams.ToStandardOutput
根据 ZerosTest
类创建一个测试套件。
suite = testsuite("ZerosTest");
创建一个不含任何插件的测试运行器。以下代码将创建一个不产生任何输出的静默运行器。
runner = testrunner("minimal");
您现在可以添加任何您选择的插件。创建一个将文本输出定向到屏幕的 ExamplePlugin
实例。
plugin = ExamplePlugin(ToStandardOutput);
将该插件添加到测试运行器中并运行测试。测试运行时,测试的名称会出现在屏幕上。
runner.addPlugin(plugin) results = runner.run(suite);
### Running test: ZerosTest/testClass(size=s2d,type=single) ### Running test: ZerosTest/testClass(size=s2d,type=double) ### Running test: ZerosTest/testClass(size=s2d,type=uint16) ### Running test: ZerosTest/testClass(size=s3d,type=single) ### Running test: ZerosTest/testClass(size=s3d,type=double) ### Running test: ZerosTest/testClass(size=s3d,type=uint16) ### Running test: ZerosTest/testSize(size=s2d) ### Running test: ZerosTest/testSize(size=s3d) ### Running test: ZerosTest/testDefaultClass ### Running test: ZerosTest/testDefaultSize ### Running test: ZerosTest/testDefaultValue
版本历史记录
在 R2013a 中推出
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)