创建 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
__________