Main Content

matlab.unittest.constraints.IsFile Class

Namespace: matlab.unittest.constraints
Superclasses: matlab.unittest.constraints.BooleanConstraint

Test if value is file

Description

The matlab.unittest.constraints.IsFile class provides a constraint to test if a value represents a file.

Creation

Description

example

c = matlab.unittest.constraints.IsFile creates a constraint to test if a value represents a file. The constraint is satisfied by a string scalar or character vector that specifies the path to an existing file. The value can be a relative path, but the relative path must be in the current folder. Otherwise, the value must be a full path. The path to the file must include the file extension.

Examples

collapse all

Test if a file exists using the IsFile constraint.

First, import the classes used in this example.

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

Create a test case for interactive testing.

testCase = TestCase.forInteractiveUse;

Create a subfolder in your current folder.

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

Verify that a file named myFile.dat does not exist in the subfolder.

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

Create the file myFile.dat and then test if it exists. The test passes.

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

Delete the file and test again. The test fails because the file no longer exists.

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"

Version History

Introduced in R2018a