Main Content

matlab.unittest.selectors.HasBaseFolder Class

Namespace: matlab.unittest.selectors

Select TestSuite array elements by base folder

Description

The matlab.unittest.selectors.HasBaseFolder class provides a selector for filtering a test suite based on base folders of tests.

The base folder of a test is the folder that contains the file defining the test. For tests defined in namespaces, the base folder is the parent of the top-level namespace folder.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

example

selector = matlab.unittest.selectors.HasBaseFolder(folder) creates a selector that selects TestSuite array elements whose base folder matches the specified value.

Input Arguments

expand all

Name of the base folder, specified as a string scalar, character vector, or matlab.unittest.constraints.Constraint object. Test selection by base folder is subject to these conditions:

  • If you specify a string scalar or character vector, the base folder of the test must be the same as the specified value. The specified value must be a full path.

  • If you specify a constraint, the base folder of the test must satisfy the constraint.

This argument sets the Constraint property.

Properties

expand all

Condition that the base folder must satisfy for the test to be included in the filtered test suite, returned as a matlab.unittest.constraints.Constraint object.

This property is set by the folder input argument:

  • If you specify a string scalar or character vector, the testing framework sets the property to the IsEqualTo constraint with the expected value as the specified base folder.

  • If you specify a constraint, the testing framework sets the property to the constraint.

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Create filtered test suites by selecting tests using the HasBaseFolder class. To simplify the test code, the test classes in this example use unconditional test failures as placeholders for unimplemented tests.

In your current folder, create a folder named myTests. Then create two folders, feature1 and feature2, in myTests.

In a file named Feature1Test.m in the feature1 folder, create the Feature1Test class.

classdef Feature1Test < matlab.unittest.TestCase
    methods (Test)
        function defaultBehavior(testCase)
            testCase.verifyFail("Add code to test default behavior.")
        end
        function otherBehavior(testCase)
            testCase.verifyFail("Add code to test nondefault behavior.")
        end
    end
end

In a file named Feature2Test.m in the feature2 folder, create the Feature2Test class.

classdef Feature2Test < matlab.unittest.TestCase
    methods (Test)
        function defaultBehavior(testCase)
            testCase.verifyFail("Add code to test default behavior.")
        end
        function otherBehavior(testCase)
            testCase.verifyFail("Add code to test nondefault behavior.")
        end
    end
end

Import the classes used in this example.

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasBaseFolder
import matlab.unittest.constraints.ContainsSubstring

Make sure that your current folder is the parent folder of myTests. Create a test suite from the myTests folder and its subfolders. Then, display the names of the TestSuite array elements. The test suite contains the tests defined in the feature1 and feature2 folders.

suite = testsuite("myTests","IncludingSubfolders",true);
disp({suite.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

Select all the tests that are defined in the feature1 folder.

suite1 = suite.selectIf(HasBaseFolder( ...
    fullfile(pwd,"myTests","feature1")));
disp({suite1.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }

Select all the tests in suite that are defined outside of feature1. The filtered test suite contains only the tests defined in the feature2 folder.

suite2 = suite.selectIf(~HasBaseFolder( ...
    fullfile(pwd,"myTests","feature1")));
disp({suite2.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

Create a filtered test suite directly from myTests and its subfolders by including only tests whose base folder contains the substring "2".

suite3 = TestSuite.fromFolder("myTests", ...
    HasBaseFolder(ContainsSubstring("2")), ...
    "IncludingSubfolders",true);
disp({suite3.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

Version History

Introduced in R2014a