matlab.unittest.selectors.HasTag class

Package: matlab.unittest.selectors

Selector for TestSuite elements determined by tag

Description

The HasTag selector filters TestSuite array elements determined by the test element tag.

Construction

matlab.unittest.selectors.HasTag constructs a selector for TestSuite elements determined by the test element tag. When you instantiate HasTag without input arguments, the resulting TestSuite array contains only elements with one or more tags.

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

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

Input Arguments

expand all

Test element tag, specified as a character vector, string scalar, or matlab.unittest.constraints.Constraint instance. If a test element tag meets the following conditions, the TestSuite contains the test:

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

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

Properties

expand all

Condition the test element tag 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 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 construction, you can filter the test suite using the 'Tag' name-value pair. For example:

s = TestSuite.fromClass(?ExampleTest,'Tag','Unit');

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

runtests('ExampleTest.m','Tag','Unit')
Introduced in R2015a