如何将matlab导入加载项,向测试运行程序添加插件

为 BankAccount 类创建测试

在您的工作文件夹下的文件中,为 BankAccount 类创建一个测试文件。

type BankAccountTest.m

classdef BankAccountTest < matlab.unittest.TestCase

% Tests the BankAccount class.

methods (TestClassSetup)

function addBankAccountClassToPath(testCase)

p = path;

testCase.addTeardown(@path,p);

addpath(fullfile(matlabroot,'help','techdoc','matlab_oop',...

'examples'));

end

end

methods (Test)

function testConstructor(testCase)

b = BankAccount(1234, 100);

testCase.verifyEqual(b.AccountNumber, 1234, ...

'Constructor failed to correctly set account number');

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

创建测试套件

在命令提示符处,基于 BankAccountTest 测试用例创建一个测试套件 ts。

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

显示不含任何插件的结果

创建一个不含任何插件的测试运行程序。

runner = matlab.unittest.TestRunner.withNoPlugins;

res = runner.run(ts);

不显示任何输出。

自定义测试运行程序

添加自定义插件 TestRunProgressPlugin。

import matlab.unittest.plugins.TestRunProgressPlugin

runner.addPlugin(TestRunProgressPlugin.withVerbosity(2))

res = runner.run(ts);

Running BankAccountTest

.....

Done BankAccountTest

__________

MATLAB 显示有关 BankAccountTest 的进度消息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值