在您的工作文件夹中,创建包含以下测试类的文件 ExampleTest.m。
classdef ExampleTest < matlab.unittest.TestCase
methods(Test)
function testOne(testCase) % Test fails
testCase.verifyEqual(5, 4, 'Testing 5==4')
end
function testTwo(testCase) % Test passes
testCase.verifyEqual(5, 5, 'Testing 5==5')
end
function testThree(testCase)
% test code
end
end
end
testOne 中的 verifyEqual 验证导致测试失败。testOne 和 testTwo 中的验证包括 matlab.unittest.diagnostics.StringDiagnostic 实例。
在命令提示符下,基于 ExampleTest 类创建测试套件。
import matlab.unittest.TestRunner
import matlab.unittest.TestSuite
import matlab.unittest.plugins.DiagnosticsOutputPlugin
import matlab.unittest.plugins.ToStandardOutput
suite = TestSuite.fromClass(?ExampleTest);
创建一个不含任何插件的测试运行程序。此代码创建一个静默运行程序并向您提供对安装的插件的完整控制权。
runner = TestRunner.withNoPlugins;
创建一个 DiagnosticsOutputPlugin,它显式指定其输出应定向到屏幕。
plugin = DiagnosticsOutputPlugin(ToStandardOutput);
将该插接添加到 TestRunner 并运行套件。
runner.addPlugin(plugin)
result = runner.run(suite);
================================================================================
Verification failed in ExampleTest/testOne.
----------------
Test Diagnostic:
----------------
Testing 5==4
---------------------
Framework Diagnostic:
---------------------
verifyEqual failed.
--> The values are not equal using "isequaln".
--> Failure table:
Actual Expected Error RelativeError
______ ________ _____ _____________
5 4 1 0.25
Actual Value:
5
Expected Value:
4
------------------
Stack Information:
------------------
In C:\work\ExampleTest.m (ExampleTest.testOne) at 4
================================================================================
Failure Summary:
Name Failed Incomplete Reason(s)
==================================================================
ExampleTest/testOne X Failed by verification.
只有测试失败才在屏幕上产生输出。