主要内容

插件生成自定义测试输出格式

这个例子展示了如何创建一个插件,该插件使用自定义格式将最终测试结果写入输出流。

创建插件

在工作文件夹的一个文件中,创建一个类,ExampleCustomPlugin,即继承自matlab.unittest.plugins.TestRunnerPlugin类。在插件类中:

  • 定义一个属性上的OutputStream实例。默认情况下,插件写入标准输出。

  • 覆盖默认的runTestSuite的方法TestRunnerPlugin以输出指示测试运行程序正在运行新的测试会话的文本。如果要写入单个日志文件,则此信息尤其有用,因为它允许您区分测试运行。

  • 覆盖默认的reportFinalizedResult的方法TestRunnerPlugin将最终的测试结果写入输出流。您可以修改打印方法以适用于您的测试日志或持续集成系统的格式输出测试结果。

classdefExampleCustomPlugin < matlab.unittest.plugins.TestRunnerPlugin属性(访问=私人)流结束方法函数p = ExampleCustomPlugin(流)如果~nargin流= matlab.unittest.plugins.ToStandardOutput;结束validateattributes(流,...“matlab.unittest.plugins.OutputStream”},{}) p.Stream = stream;结束结束方法(访问=保护)函数runTestSuite(插件,pluginData) plugin.Stream.print ('\n——%s的新测试会话——\n'...runTestSuite@ char (datetime))...matlab.unittest.plugins.TestRunnerPlugin(插件,pluginData);结束函数reportFinalizedResult(plugin,pluginData) thisResult = pluginData. testresult;如果thisResult。通过状态=“通过”elseifthisResult。失败的状态=“失败”elseifthisResult。不完整的status =“跳过”结束plugin.Stream.print (...' YPS公司-测试%s ### - %s在%f秒。\n'...状态、thisResult.Name reportFinalizedResult@ thisResult.Duration)...pluginData matlab.unittest.plugins.TestRunnerPlugin(插件)结束结束结束

创建测试类

在您的工作文件夹中,创建文件ExampleTest.m包含以下测试类。在这个测试类中,有两个测试通过,其他测试导致验证或假设失败。

classdefExampleTest < matlab.unittest.TestCase方法(测试)函数testOne (testCase) testCase.assertGreaterThan(5、1)结束函数testTwo (testCase) wrongAnswer =“错”;testCase.verifyEmpty (wrongAnswer“不空”) testCase.verifyClass (wrongAnswer“双”“不翻”结束函数testThree (testCase) testCase.assumeEqual(7 * 2, 13日的值不等于结束函数testFour (testCase) testCase.verifyEqual(3 + 2、5);结束结束结束

将插件添加到测试运行程序并运行测试

在命令提示符下,从ExampleTest类,并创建一个测试运行器。

进口matlab.unittest.TestSuite进口matlab.unittest.TestRunner套件= TestSuite.fromClass (? ExampleTest);跑步者= TestRunner.withNoPlugins;

的实例创建ExampleCustomPlugin并将其添加到测试运行程序中。运行测试。

进口matlab.unittest.plugins.ToFile帧=“YPS_test_results.txt”;p = ExampleCustomPlugin(去整理(帧));run . addplugin (p) result = runner.run(suite);

查看输出文件的内容。

类型(帧)
-新的测试会话在2015年1月26日10:41:24 - ### YPS公司-测试通过### - ExampleTest/ teststone在0.123284秒。在0.090363秒内完成ExampleTest/testTwo测试。在0.518044秒内完成ExampleTest/testThree。在0.020599秒内通过ExampleTest/testFour测试。

重新运行不完整的使用相同的测试运行程序进行测试。查看输出文件的内容。

suiteFiltered =套件([result.Incomplete]);result2 = runner.run (suiteFiltered);类型(帧)
-新的测试会话在2015年1月26日10:41:24 - ### YPS公司-测试通过### - ExampleTest/ teststone在0.123284秒。在0.090363秒内完成ExampleTest/testTwo测试。在0.518044秒内完成ExampleTest/testThree。在0.020599秒内通过ExampleTest/testFour测试。—新的测试会话在2015年1月26日10:41:58—### YPS公司-测试跳过### - ExampleTest/testThree在0.007892秒。

另请参阅

|||

相关的话题