applyFixture
类: matlab.unittest.fixtures.Fixture
包: matlab.unittest.fixtures
设置脚手架以将工作委派给另一个脚手架
说明
输入参数
fixture1
— 主要脚手架
matlab.unittest.fixtures.Fixture
的实例
主要脚手架,指定为 matlab.unittest.fixtures.Fixture
的实例。
fixture2
— 应用于主要脚手架的脚手架
matlab.unittest.fixtures.Fixture
的实例
用于设置主要脚手架的脚手架,指定为 matlab.unittest.fixtures.Fixture
的实例。主要脚手架将工作委派给此脚手架。
示例
委派工作以隐藏警告
构造一个脚手架,该脚手架从搜索路径中删除一个文件夹,并在您尝试删除不在该路径上的文件夹时不显示警告。该脚手架使用 SuppressedWarningsFixture
脚手架来禁用指定警告。作为拆解 RemoveFolderFromPathFixture
的一部分,测试框架还会适时清除 SuppressedWarningsFixture
,从而将警告状态还原为其先前的值。
classdef RemoveFolderFromPathFixture < matlab.unittest.fixtures.Fixture properties (SetAccess = immutable) Folder (1,1) string end methods function fixture = RemoveFolderFromPathFixture(folder) fixture.Folder = folder; end function setup(fixture) import matlab.unittest.fixtures.SuppressedWarningsFixture % Delegate to SuppressedWarningsFixture to suppress display of warnings. fixture.applyFixture(SuppressedWarningsFixture('MATLAB:rmpath:DirNotFound')); % Remove the folder from the path. originalPath = path; fixture.addTeardown(@()path(originalPath)); rmpath(fixture.Folder); end end methods (Access = protected) function bool = isCompatible(fixture1,fixture2) bool = fixture1.Folder == fixture2.Folder; end end end
委托工作以创建临时文本文件
构建一个脚手架,该脚手架用于创建临时文本文件。该脚手架使用 TemporaryFolderFixture
脚手架构建一个临时文件夹,然后在该文件夹中创建一个文本文件。作为拆解 TemporaryTextFileFixture
的一部分,框架还会适时清除 TemporaryFolderFixture
,从而删除该文件夹及其中的内容。
classdef TemporaryTextFileFixture < matlab.unittest.fixtures.Fixture properties (SetAccess = private) File end methods function setup(fixture) import matlab.unittest.fixtures.TemporaryFolderFixture % Delegate to TemporaryFolderFixture to create a temporary folder. tempFixture = fixture.applyFixture(TemporaryFolderFixture); fixture.File = fullfile(tempFixture.Folder,'file.txt'); fid = fopen(fixture.File,'wt'); fixture.fatalAssertNotEqual(fid,-1); status = fclose(fid); fixture.fatalAssertEqual(status,0); end end end
版本历史记录
在 R2016a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)