Main Content

matlab.unittest.selectors.HasName class

Package: matlab.unittest.selectors

Selector for TestSuite elements determined by name

Description

The HasName selector filters TestSuite array elements determined by the test element name.

Construction

matlab.unittest.selectors.HasName(n) constructs a selector for TestSuite elements determined by the test element name, n. You can specify the name as a character vector, a string scalar, or an instance of the matlab.unittest.constraints.Constraint class. If the specified name, n, is a character vector or string scalar, the testing framework creates an IsEqualTo constraint with n as the expected value.

For a test element to be included in the suite, the test element must have the same name as the specified name.

Input Arguments

expand all

Test element name, 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 n is a character vector or string scalar, the test element’s name must exactly match the specified name.

  • If n is a constraint, the test element’s name must satisfy the specified constraint.

Properties

Constraint

Condition the test element name 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 the following test class in a file, ExampleTest.m, in your current folder.

classdef ExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testPathAdd(testCase)
            % test code
        end
        function testOne(testCase)
            % test code
        end
         function testTwo(testCase)
            % test code
        end
    end
end

At the command prompt, create a test suite from the ExampleTest.m file and examine the contents.

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasName
import matlab.unittest.constraints.EndsWithSubstring

suite = TestSuite.fromFile('ExampleTest.m');
{suite.Name}
ans =

  1×3 cell array

    {'ExampleTest/testPathAdd'}    {'ExampleTest/testOne'}    {'ExampleTest/testTwo'}

The suite contains three tests.

Select all the test suite elements that have the name 'ExampleTest/testPathAdd', and examine the contents.

s1 = suite.selectIf(HasName('ExampleTest/testPathAdd'));
{s1.Name}
ans =

  1×1 cell array

    {'ExampleTest/testPathAdd'}

The filtered test suite only contains one test element.

Select all the test suite elements that end in either 'One' or 'Two', and examine the contents.

s1 =  suite.selectIf(HasName(EndsWithSubstring('One')) | ...
    HasName(EndsWithSubstring('Two')));
{s1.Name}
ans =

  1×2 cell array

    {'ExampleTest/testOne'}    {'ExampleTest/testTwo'}

At the time of the test suite construction, create a test suite that only contains tests with the substring 'One'.

import matlab.unittest.constraints.ContainsSubstring
s2 = TestSuite.fromFile('ExampleTest.m',...
    HasName(ContainsSubstring('One')))
s2 = 

  Test with properties:

                  Name: 'ExampleTest/testOne'
         ProcedureName: 'testOne'
             TestClass: "ExampleTest"
            BaseFolder: 'C:\work'
      Parameterization: [0×0 matlab.unittest.parameters.EmptyParameter]
    SharedTestFixtures: [0×0 matlab.unittest.fixtures.EmptyFixture]
                  Tags: {1×0 cell}

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.
Introduced in R2014a