Main Content

本页翻译不是最新的。点击此处可查看最新英文版本。

matlab.mock.TestCase 类

命名空间: matlab.mock
超类: matlab.unittest.TestCase

TestCase,用来编写使用模拟框架的测试

描述

使用 matlab.mock.TestCase 类编写使用模拟框架的测试。matlab.mock.TestCase 派生自 matlab.unittest.TestCase 类。

构造

测试框架可以构造 matlab.mock.TestCase 实例。

方法

assertAccessed断言属性已被访问过
assertCalled断言使用特定输入值调用过方法
assertNotAccessed断言属性未被访问过
assertNotCalled断言未使用特定输入值调用过方法
assertNotSet断言属性未经过设置
assertSet断言属性已经过设置
assignOutputsWhen定义方法调用或属性访问的返回值
assumeAccessed假定属性已被访问过
assumeCalled假定使用特定输入值调用过方法
assumeNotAccessed假定属性未被访问过
assumeNotCalled假定未使用特定输入值调用过方法
assumeNotSet假定属性未经过设置
assumeSet假定属性已经过设置
clearMockHistory清除 mock 对象交互的历史记录
createMock创建 mock 对象
fatalAssertAccessed致命断言属性已被访问过
fatalAssertCalled致命断言使用特定输入值调用过方法
fatalAssertNotAccessed致命断言属性未被访问过
fatalAssertNotCalled致命断言未使用特定输入值调用过方法
fatalAssertNotSet致命断言属性未经过设置
fatalAssertSet致命断言属性已经过设置
forInteractiveUse创建供 mock 对象交互使用的 TestCase
getMockHistoryTestCase 实例返回 mock 交互的历史记录
returnStoredValueWhen访问属性时返回所存储的值
storeValueWhen设置属性时存储值
throwExceptionWhen对方法调用或属性交互引发异常
verifyAccessed确认属性已被访问过
verifyCalled确认使用特定输入值调用过方法
verifyNotAccessed确认属性未被访问过
verifyNotCalled确认未使用特定输入值调用过方法
verifyNotSet确认属性未经过设置
verifySet确认属性已经过设置

继承的方法

addTeardown向测试用例动态添加拆解代码
applyFixture 将脚手架与测试用例配合使用
createTemporaryFolder Create temporary folder
forInteractiveUse创建供交互测试的测试用例
getSharedTestFixtures 提供对共享测试脚手架的访问权限
log在测试执行期间记录诊断信息
onFailure动态添加测试失败的诊断
run运行与测试用例对应的测试

TestCase 类还从以下类继承方法:

matlab.unittest.qualifications.Assertable用来确认测试先决条件的验证
matlab.unittest.qualifications.Assumable用于过滤测试内容的验证
matlab.unittest.qualifications.FatalAssertable用于中止测试执行的验证
matlab.unittest.qualifications.Verifiable产生软失败条件的验证

复制语义

句柄。要了解句柄类如何影响复制操作,请参阅复制对象

示例

全部折叠

使用 mock 编写一个测试。

import matlab.unittest.constraints.IsLessThan;
testCase = matlab.mock.TestCase.forInteractiveUse;

% Create a mock for a bank account class
[mock, behavior] = testCase.createMock('AddedMethods',["deposit" "isOpen"]);

% Set up behavior
testCase.throwExceptionWhen(behavior.deposit(IsLessThan(0)), ...
    MException('Account:deposit:Negative', ...
    'Deposit amount must be positive.'));

% Use mock object
mock.deposit(100);
testCase.verifyError(@() mock.deposit(-10), 'Account:deposit:Negative');

% Passing verifications
testCase.verifyCalled(behavior.deposit(100),...
    'A $100 deposit should have been made.');
testCase.assertNotCalled(behavior.deposit(0));
testCase.assertCalled(behavior.deposit(IsLessThan(0)));

% Failing assertion
testCase.assertCalled(withExactInputs(behavior.isOpen));

版本历史记录

在 R2017a 中推出