Main Content

matlab.unittest.selectors.HasBaseFolder class

Package: matlab.unittest.selectors

Selector for TestSuite elements determined by folder

Description

The HasBaseFolder selector filters TestSuite array elements determined by the name of the folder that contains the file that defines the test class or function.

Construction

matlab.unittest.selectors.HasBaseFolder(f) constructs a selector for TestSuite elements determined by the folder, f, which contains the file that defines the test class or function. You can specify the base folder as a character vector, a string scalar, or an instance of the matlab.unittest.constraints.Constraint class. If the specified base folder, f, is a character vector or string scalar instead of a Constraint, the testing framework creates an IsEqualTo constraint to select test elements from the base folder f.

For a test element to be included in the suite, the file that defines it must be contained in the specified base folder. For test classes defined in packages, the base folder is the parent of the top-level package folder. The base folder never contains any folders that start with '+' or '@'.

Input Arguments

expand all

Base folder, specified as a character vector, string scalar, or matlab.unittest.constraints.Constraint instance. The following conditions must be satisfied for the test element to be selected for the TestSuite:

  • If f is a character vector or string scalar, the test element’s base folder must exactly match the specified folder.

  • If f is a constraint, the test element’s base folder must satisfy the specified constraint.

Properties

Constraint

Condition the base folder must satisfy to be included in the test suite, specified as an instance of the matlab.unittest.constraints.Constraint class.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a folder named MyTests in your current folder. In this folder, create two subfolders, Feature1 and Feature2.

In a new file, Feature1_Test.m, in the Feature1 subfolder, create the following test class.

classdef Feature1_Test < matlab.unittest.TestCase
    methods (Test)
        function testA1(testCase)
            % test code
        end
        function testB1(testCase)
            % test code
        end
    end
end

In a new file, Feature2_Test.m, in the Feature2 subfolder, create the following test class.

classdef Feature2_Test < matlab.unittest.TestCase
    methods (Test)
        function testA2(testCase)
            % test code
        end
        function testB2(testCase)
            % test code
        end
    end
end

If necessary, set your current folder to the folder above MyTests. At the command prompt, create a test suite from the MyTests folder and examine the contents.

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

suite = TestSuite.fromFolder('MyTests','IncludingSubfolders',true);
{suite.Name}
ans =

  1×4 cell array

    {'Feature1_Test/testA1'}    {'Feature1_Test/testB1'}    {'Feature2_Test/testA2'}    {'Feature2_Test/testB2'}

The suite contains the four tests from the two test files.

Select all test suite elements for classes that are defined in the 'Feature1' folder.

s1 = suite.selectIf(HasBaseFolder(...
    fullfile(pwd,'MyTests','Feature1')));
{s1.Name}
ans =

  1×2 cell array

    {'Feature1_Test/testA1'}    {'Feature1_Test/testB1'}

The filtered test suite contains only test elements from the Feature1 folder.

Select all the test suite elements for classes that are defined in folders that do not contain 'Feature1', and then examine the contents.

s2 = suite.selectIf(~HasBaseFolder(...
    fullfile(pwd,'MyTests','Feature1')));
{s2.Name}
ans =

  1×2 cell array

    {'Feature2_Test/testA2'}    {'Feature2_Test/testB2'}

The filtered test suite only contains test elements from the Feature2 folder.

Alternatively, to generate a filtered suite directly, pass the selector to the TestSuite.fromFolder method.

s2 = TestSuite.fromFolder('MyTests',...
    ~HasBaseFolder(ContainsSubstring('Feature1')),...
    'IncludingSubfolders',true);
{s2.Name}
ans =

  1×2 cell array

    {'Feature2_Test/testA2'}    {'Feature2_Test/testB2'}
Introduced in R2014a