needsReset
类: matlab.unittest.fixtures.Fixture
命名空间: matlab.unittest.fixtures
确定共享测试脚手架是否需要重置
说明
tf = needsReset( 向测试框架报告共享测试 fixture)fixture 的有效性。如果 fixture 无效并且需要重置,则该方法返回逻辑值 1 (true)。否则,将返回逻辑值 0 (false)。如果由脚手架配置的测试环境状态在整个测试会话期间保持不变,则共享测试脚手架有效。
对于使用共享测试脚手架的测试类,每当测试运行器切换到后续类时,框架都会调用 needsReset 方法。如果该方法返回 true,则框架会自动拆解共享测试脚手架,并针对后续类对其进行设置。框架执行由 teardown 或 addTeardown 方法定义的操作来拆解失效的脚手架,并执行由 setup 方法定义的操作来设置脚手架。因此,您的 needsReset 实现不能包含执行脚手架设置或拆解操作的代码。
输入参数
要验证的共享测试脚手架,指定为 matlab.unittest.fixtures.Fixture 类的实例。
示例
创建一个脚手架以从 MATLAB® 搜索路径中删除一个文件夹,并指示测试框架在脚手架失效时重置环境状态。然后,在多个类中运行测试时,将脚手架用作共享测试脚手架。
此示例假设路径中存在当前文件夹的子文件夹 helperFiles。如果该子文件夹不存在,请创建一个,并确保它在路径中。
if ~isfolder('helperFiles') mkdir helperFiles end addpath('helperFiles')
在当前文件夹的一个文件中,创建一个名为 RemoveFolderFromPathFixture 的脚手架,它通过从路径中删除一个文件夹来设置环境状态。为了确保将该脚手架用作共享测试脚手架的所有测试类的环境状态相同,请覆盖 needsReset 方法。当测试运行器切换到后续类时,如果指定的文件夹在路径中,则该方法返回 true。
classdef RemoveFolderFromPathFixture < matlab.unittest.fixtures.Fixture properties (SetAccess = immutable) Folder (1,1) string % Full path to the folder end methods function fixture = RemoveFolderFromPathFixture(folder) fixture.Folder = folder; end function setup(fixture) originalPath = path; fixture.addTeardown(@()path(originalPath)); rmpath(fixture.Folder) end end methods (Access = protected) function tf = isCompatible(fixture1,fixture2) tf = fixture1.Folder == fixture2.Folder; end function tf = needsReset(fixture) foldersOnPath = split(path,pathsep); tf = ismember(fixture.Folder,foldersOnPath); end end end
在当前文件夹中,创建三个使用 RemoveFolderFromPathFixture 作为共享测试脚手架的测试类。
在名为 SampleTestA.m 的文件中,创建 SampleTestA 类。
classdef (SharedTestFixtures = { ... RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ... SampleTestA < matlab.unittest.TestCase methods (Test) function test1(testCase) import matlab.unittest.constraints.ContainsSubstring f = testCase.getSharedTestFixtures; testCase.assertThat(path,~ContainsSubstring(f.Folder)) end end end
在名为 SampleTestB.m 的文件中,创建 SampleTestB 类。类中的测试将 helperFiles 添加到路径中。
classdef (SharedTestFixtures = { ... RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ... SampleTestB < matlab.unittest.TestCase methods (Test) function test1(testCase) import matlab.unittest.constraints.ContainsSubstring f = testCase.getSharedTestFixtures; addpath('helperFiles') testCase.assertThat(path,ContainsSubstring(f.Folder)) end end end
在名为 SampleTestC.m 的文件中,创建 SampleTestC 类。
classdef (SharedTestFixtures = { ... RemoveFolderFromPathFixture(fullfile(pwd,'helperFiles'))}) ... SampleTestC < matlab.unittest.TestCase methods (Test) function test1(testCase) import matlab.unittest.constraints.ContainsSubstring f = testCase.getSharedTestFixtures; testCase.assertThat(path,~ContainsSubstring(f.Folder)) end end end
创建一个测试套件并运行测试。为了验证共享测试脚手架,当测试运行器切换到 SampleTestB 和 SampleTestC 时,测试框架调用 needsReset 方法。
suite = [testsuite('SampleTestA') testsuite('SampleTestB') ... testsuite('SampleTestC')]; runner = matlab.unittest.TestRunner.withTextOutput; results = runner.run(suite);
Setting up RemoveFolderFromPathFixture Done setting up RemoveFolderFromPathFixture __________ Running SampleTestA . Done SampleTestA __________ Running SampleTestB . Done SampleTestB __________ Tearing down RemoveFolderFromPathFixture Done tearing down RemoveFolderFromPathFixture __________ Setting up RemoveFolderFromPathFixture Done setting up RemoveFolderFromPathFixture __________ Running SampleTestC . Done SampleTestC __________ Tearing down RemoveFolderFromPathFixture Done tearing down RemoveFolderFromPathFixture __________
SampleTestA 不会破坏 RemoveFolderFromPathFixture 设置的环境状态。因此,框架使用已设置的脚手架来运行 SampleTestB。但是,SampleTestB 通过向路径添加 helperFiles 来破坏环境状态。该框架会拆解脚手架,并在调用 SampleTestB 和 SampleTestC 之间设置脚手架。
版本历史记录
在 R2020b 中推出
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)