文档

将插件添加到测试运行器

这个例子展示了如何向测试运行器中添加插件。的matlab.unittest.plugins.TestRunProgressPlugin显示关于测试用例的进度消息。这个插件是matlab.unittest包中。MATLAB®使用它作为默认的测试运行程序。

为BankAccount类创建一个测试

在工作文件夹中的一个文件中,为BankAccount类。

类型BankAccountTest.m
classdef BankAccountTest < matlab.unittest.TestCase %测试BankAccount类函数addBankAccountClassToPath(testCase) p = path;@ path, testCase.addTeardown (p);目录(fullfile (matlabroot,‘帮助’,‘techdoc’,‘matlab_oop’,……“示例”));函数testConstructor(testCase) b = BankAccount(1234,100);testCase.verifyEqual (b。AccountNumber, 1234,…'构造器未能正确设置帐号');testCase.verifyEqual (b。AccountBalance, 100, ... 'Constructor failed to correctly set account balance'); end function testConstructorNotEnoughInputs(testCase) import matlab.unittest.constraints.Throws; testCase.verifyThat(@()BankAccount, ... Throws('MATLAB:minrhs')); end function testDesposit(testCase) b = BankAccount(1234, 100); b.deposit(25); testCase.verifyEqual(b.AccountBalance, 125); end function testWithdraw(testCase) b = BankAccount(1234, 100); b.withdraw(25); testCase.verifyEqual(b.AccountBalance, 75); end function testNotifyInsufficientFunds(testCase) callbackExecuted = false; function testCallback(~,~) callbackExecuted = true; end b = BankAccount(1234, 100); b.addlistener('InsufficientFunds', @testCallback); b.withdraw(50); testCase.assertFalse(callbackExecuted, ... 'The callback should not have executed yet'); b.withdraw(60); testCase.verifyTrue(callbackExecuted, ... 'The listener callback should have fired'); end end end

创建测试套件

在命令提示符下,创建一个测试套件,ts,由bankaccount测试用例。

ts = matlab.unittest.TestSuite.fromClass(?BankAccountTest);

显示没有插件的结果

创建一个没有插件的测试运行程序。

runner = matlab.unittest. testrunn . withnoplugins;Res = runner.run(ts);

没有显示任何输出。

自定义测试运行器

添加自定义插件,TestRunProgressPlugin

进口matlab.unittest.plugins.TestRunProgressPluginrunner.addPlugin(testrunprogressplugin . withverbose (2)) res = runner.run(ts);
运行BankAccountTest .....完成BankAccountTest __________

MATLAB显示进度消息bankaccount

另请参阅

这个话题有帮助吗?