Main Content

matlab.unittest.selectors.HasProcedureName class

Package: matlab.unittest.selectors

Select TestSuite array elements by procedure name

Description

The matlab.unittest.selectors.HasProcedureName class provides a selector for filtering the test suite based on the procedure name.

Construction

selector = matlab.unittest.selectors.HasProcedureName(name) creates a selector that selects TestSuite array elements that have the specified procedure name.

For a Test element to be included in the filtered test 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 string scalar, character vector, or matlab.unittest.constraints.Constraint object. In a class-based test, the name of a test procedure is the name of a Test method that contains the test. In a function-based test, it is the name of a local function that contains the test. In a script-based test, it is a name generated from the test section title. Unlike the name of a test suite element, the name of a test procedure does not include any package name, file name, or information about parameterization.

Example: "Test1"

Example: matlab.unittest.constraints.ContainsSubstring("Test")

Properties

expand all

Condition the procedure name must satisfy to be included in the filtered test suite, specified as a matlab.unittest.constraints.Constraint object.

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 creation, you can filter the test suite using the ProcedureName name-value argument. For example, the following lines of code are functionally equivalent.

s = matlab.unittest.TestSuite.fromClass(?ExampleTest,"ProcedureName","Test1");
s = testsuite("ExampleTest.m","ProcedureName","Test1");

Version History

Introduced in R2017a