Main Content

matlab.unittest.selectors.HasTag Class

Namespace: matlab.unittest.selectors

Select TestSuite array elements by test tag

Description

The matlab.unittest.selectors.HasTag class provides a selector for filtering the test suite based on test tags.

Construction

selector = matlab.unittest.selectors.HasTag creates a selector that selects any tagged TestSuite array elements.

selector = matlab.unittest.selectors.HasTag(tag) create a selector that selects TestSuite array elements that have the specified test tag. You can specify tag as a string scalar, character vector, or matlab.unittest.constraints.Constraint object. If tag is a string scalar or character vector, then the testing framework creates an IsEqualTo constraint to select Test elements tagged with the specified value.

For a Test element to be included in the filtered test suite, the Test element must be tagged with the specified string scalar or character vector or with a value that satisfies the specified constraint.

Input Arguments

expand all

Test tag, specified as a string scalar, character vector, or matlab.unittest.constraints.Constraint object. If a tagged test meets the following conditions, the filtered test suite contains the test:

  • If tag is a character vector or string scalar, the test tag is the specified value.

  • If tag is a constraint, the test tag is a value that satisfies the specified constraint.

Properties

expand all

Condition the test tag 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 testA (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'Unit'})
        function testB (testCase)
            % test code
        end
        function testC (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'Unit','FeatureA'})
        function testD (testCase)
            % test code
        end
    end
    methods (Test, TestTags = {'System','FeatureA'})
        function testE (testCase)
            % test code
        end
    end
end

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

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasTag

suite = TestSuite.fromClass(?ExampleTest)
suite = 

  1×5 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 3 Unique Tags.

Click the hyperlink for 3 Unique Tags to display all the tags in the suite.

        Tag     
    ____________

    {'FeatureA'}
    {'System'  }
    {'Unit'    }   

Select all the test suite elements that have the tag 'Unit'.

s1 = suite.selectIf(HasTag('Unit'))
s1 = 

  1×3 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 2 Unique Tags.

Select all the test suite elements that do not contain the tag 'FeatureA'.

s2 =  suite.selectIf(~HasTag('FeatureA'));
{s2.Name}
ans =

  1×3 cell array

    {'ExampleTest/testB'}    {'ExampleTest/testC'}    {'ExampleTest/testA'}

Select all the test suite elements that have no tags.

s3 =  suite.selectIf(~HasTag)
s3 = 

  Test with properties:

                  Name: 'ExampleTest/testA'
         ProcedureName: 'testA'
             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 HasTag selector for maximum flexibility to create test suites from tags. Alternatively, at the time of test suite creation, you can filter the test suite using the Tag name-value argument. For example:

s = matlab.unittest.TestSuite.fromClass(?ExampleTest,"Tag","Unit");

You also can select and run tagged tests using the Tag name-value argument with the runtests function. For example:

runtests("ExampleTest.m","Tag","Unit");

Version History

Introduced in R2015a