Main Content

matlab.unittest.constraints.IsFile 类

命名空间: matlab.unittest.constraints
超类: matlab.unittest.constraints.BooleanConstraint

测试值是否为文件

描述

matlab.unittest.constraints.IsFile 类提供一个约束来测试值是否表示文件。

创建对象

描述

示例

c = matlab.unittest.constraints.IsFile 创建一个约束来测试值是否表示文件。指定现有文件路径的字符串标量或字符向量满足该约束。该值可以是相对路径,但相对路径必须在当前文件夹中。否则,该值必须为完整路径。文件路径必须包括文件扩展名。

示例

全部折叠

使用 IsFile 约束测试文件是否存在。

首先,导入此示例中使用的类。

import matlab.unittest.TestCase
import matlab.unittest.constraints.IsFile

创建一个供交互测试的测试用例。

testCase = TestCase.forInteractiveUse;

在当前文件夹中创建一个子文件夹。

folderName = "myFolder_" + string( ...
    datetime("now",Format="yyyyMMdd'T'HHmmss"));
mkdir(folderName)

验证该子文件夹中不存在名为 myFile.dat 的文件。

filename = folderName + filesep + "myFile.dat";
testCase.verifyThat(filename,~IsFile)
Verification passed.

创建文件 myFile.dat,然后测试它是否存在。测试通过。

writematrix(magic(20),filename)
testCase.verifyThat(filename,IsFile)
Verification passed.

删除该文件并再次测试。测试失败,因为文件不再存在。

delete(filename)
testCase.verifyThat(filename,IsFile)
Verification failed.
    ---------------------
    Framework Diagnostic:
    ---------------------
    IsFile failed.
    --> Value does not point to an existing file.
    --> Current folder during evaluation:
            'C:\work'
    
    Actual Value:
        "myFolder_20220622T160205\myFile.dat"

版本历史记录

在 R2018a 中推出