Main Content

matlab.unittest.selectors.HasProcedureName class

Package: matlab.unittest.selectors

Selector for TestSuite elements determined by procedure name

Description

The HasProcedureName selector filters TestSuite array elements determined by the procedure name. The name of the test procedure does not include any class or package name or information about parameterization. In a class-based test, the procedure name is the name of the test method. In a function-based test, it is the name of the local function that contains the test. In a script-based test, it is a name generated from the test section title.

Construction

matlab.unittest.selectors.HasProcedureName(n) constructs a selector for TestSuite elements determined by the procedure name, n.

For a test element to be included in the suite, the procedure name of the test element must match the specified name or satisfy the specified constraint.

Input Arguments

expand all

Procedure name specified as a character vector, string scalar, or matlab.unittest.constraints.Constraint instance. The name of the test procedure does not include any class or package name or information about parameterization. In a class-based test, the procedure name is the name of the test method. In a function-based test, it is the name of the local function that contains the test. In a script-based test, it is the name of a test section.

Example: "Test1"

Example: ContainsSubstring('Test')

Properties

expand all

Condition the procedure 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.HasProcedureName;
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 procedure name testPathAdd, and examine the contents.

s1 = suite.selectIf(HasProcedureName("testPathAdd"))
s1 = 

  Test with properties:

                  Name: 'ExampleTest/testPathAdd'
         ProcedureName: 'testPathAdd'
             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.

The filtered test suite only contains one test element.

Select all the test suite elements with a procedure name that ends in either 'One' or 'Two', and examine the contents.

s1 =  suite.selectIf(HasProcedureName(EndsWithSubstring('One')) | ...
    HasProcedureName(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',...
    HasProcedureName(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.

Alternatives

Use the HasProcedureName selector for maximum flexibility to create test suites from procedure names. Alternatively, at the time of test suite construction, you can filter the test suite using the 'ProcedureName' name-value pair. For example, the following lines of code are functionally equivalent.

s = TestSuite.fromClass(?ExampleTest,'ProcedureName','Test1');
s = testsuite('ExampleTest.m','ProcedureName','Test1');

Version History

Introduced in R2017a