先决与 matlab,用来确认测试先决条件的验证

创建 DocPolynomSaveLoadTest 测试用例。在此示例的后续步骤中,请参阅以下 DocPolynomSaveLoadTest 测试用例。这些步骤高亮显示 testSaveLoad 函数中的特定代码;代码语句不适合在类定义文件上下文外部执行。

classdef DocPolynomSaveLoadTest < matlab.unittest.TestCase

methods (TestClassSetup)

function addDocPolynomClassToPath(testCase)

origPath = path;

testCase.addTeardown(@path, origPath);

addpath(fullfile(matlabroot, ...

'help', 'techdoc', 'matlab_oop', 'examples'));

end

end

methods (Test)

function testSaveLoad(testCase)

import matlab.unittest.diagnostics.Diagnostic;

%% Phase 1: Setup

% Create a temporary working folder

tempFolder = tempname;

[success, message] = mkdir(tempFolder);

testCase.assertTrue(success, ...

Diagnostic.join('Could not create temporary folder.',...

message));

testCase.addTeardown(@() testCase.cleanUpTemporaryFolder(...

tempFolder));

% Change to the temporay folder and register the

% teardown, which restores the original folder

origFolder = pwd;

testCase.addTeardown(@cd, origFolder);

cd(tempFolder);

%% Phase 2: Exercise

% Save the instance to a mat file.

p = DocPolynom([1, 0, 1]);

save('DocPolynomFile', 'p');

% Validate Precondition. Save resulted in valid .mat file

testCase.assertEqual(exist('DocPolynomFile.mat','file'),...

2, Diagnostic.join(...

'mat file was not saved correctly.',@() dir(pwd)));

loaded = load('DocPolynomFile');

%% Phase 3: Verify

testCase.verifyEqual(loaded.p, p,...

'Loaded polynom did not equal original polynom.');

%% Phase 4: Teardown

% Done inline via calls to addTeardown at the points

% at which the state was changed.

end

end

methods(Access=private)

function cleanUpTemporaryFolder(testCase,tempFolder)

% Clean up the temporary folder and fatally assert

% that it was correctly cleaned up.

import matlab.unittest.diagnostics.Diagnostic;

[success, message] = rmdir(tempFolder, 's');

testCase.fatalAssertTrue(success, ...

Diagnostic.join('Could not remove temporary folder.',...

message));

end

end

end

要在“运行 DocPolynomSaveLoadTest 测试用例”中执行 MATLAB® 命令,请将 DocPolynomSaveLoadTest.m 文件添加到 MATLAB 路径上的文件夹。

testSaveLoad 函数包括以下阶段:

阶段 1:设置 - 创建并验证先决条件代码。

阶段 2:执行 - 创建一个 DocPolynom 对象并将其保存到 MAT 文件中。

阶段 3:验证 - 测试是否成功保存了对象。

阶段 4:拆解 - 执行拆解代码。

定义阶段 1 先决条件。对于此测试,使用临时文件夹创建 DocPolynom 对象。继续进行此测试的先决条件是以下命令成功执行。

tempFolder = tempname;

[success, message] = mkdir(tempFolder);

测试 mkdir 函数的结果。使用 assertTrue 方法测试 mkdir success 参数有无错误。如果出现断言,testSaveLoad 测试方法的其余部分无效,并且测试标记为 Incomplete。

testCase.assertTrue(success, ...

Diagnostic.join('Could not create the temporary folder.', ...

message))

如果 mkdir 函数失败,MATLAB 将显示诊断消息“Could not create the temporary folder”以及 mkdir message 参数的内容。

添加拆解脚手架代码。创建临时文件夹是设置代码,这需要对 rmdir 函数进行相应调用以将 MATLAB 恢复为原始状态。使用 addTeardown 方法可确保即使在测试方法中引发异常,拆解代码也会执行。这使测试满足异常安全条件。

testCase.addTeardown(@() testCase.cleanUpTemporaryFolder(tempFolder))

将拆解代码放在辅助函数中。虽然 addTeardown 语句出现在与 mkdir 设置语句相同的代码块中,但 cleanUpTemporaryFolder 代码将在测试方法的阶段 4 中执行。

在 DocPolynomSaveLoadTest 测试用例中,辅助函数 cleanUpTemporaryFolder 执行 rmdir 函数。

定义用于创建有效 MAT 文件的先决条件。用于验证 DocPolynom 对象是否正确保存和加载的先决条件是 MAT 文件 DocPolynomFile.mat 已成功创建。Phase

2: Exercise 块中的以下代码测试此条件。如果出现断言,testSaveLoad 测试方法的其余部分无效,并且测试标记为 Failed 和 Incomplete。

testCase.assertEqual(exist('DocPolynomFile.mat','file'), 2, ...

Diagnostic.join('The mat file was not saved correctly.', ...

@() dir(pwd)))

如果未创建文件,MATLAB 将显示诊断消息、The mat file was not saved correctly 以及临时文件夹的内容。

运行 DocPolynomSaveLoadTest 测试用例。

tc = DocPolynomSaveLoadTest;

run(tc);

Running DocPolynomSaveLoadTest

.

Done DocPolynomSaveLoadTest

__________

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值