Documentation Center

  • Trial Software
  • Product Updates

selectIf

Class: matlab.unittest.TestSuite
Package: matlab.unittest

Select test suite elements that satisfy conditions

Syntax

  • newsuite = selectIf(suite,s) example
  • newsuite = selectIf(suite,Name,Value) example

Description

example

newsuite = selectIf(suite,s) selects from suite the test elements that satisfy the conditions specified by the selector, s, and returns them in the TestSuite array, newsuite.

example

newsuite = selectIf(suite,Name,Value) creates a TestSuite array with additional options specified by one or more Name,Value pair arguments.

Input Arguments

suite

Set of tests, specified as a matlab.unittest.TestSuite array.

s

Selector, specified as an instance of a class from the matlab.unittest.selector package.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

'Name'

String indicating the name of the suite element. To include a test element in the suite, the Name property of the test element must match the specified name. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character.

'ParameterProperty'

String indicating the name of a property that defines a parameter used by the test suite element. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character.

'ParameterName'

String indicating the name of a parameter used by the test suite element. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character.

'BaseFolder'

String indicating the name of the folder that contains the file defining the test class or function. For a test element to be included in the suite, the test element must be contained in the specified base folder. Use the wildcard character, *, to match any number of characters. Use the question mark character, ?, to match to exactly one character. For test classes defined in packages, the base folder is the parent of the top-level package folder.

Examples

expand all

Select Test Elements Using Selector

In your working folder, create the file ExampleTest.m containing the following test class.

classdef (SharedTestFixtures={...
        matlab.unittest.fixtures.PathFixture(fullfile(...
        matlabroot, 'help', 'techdoc', 'matlab_oop', 'examples'))})...
        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 class.

import matlab.unittest.TestSuite;
import matlab.unittest.selectors.HasSharedTestFixture;
import matlab.unittest.selectors.HasName;
import matlab.unittest.fixtures.PathFixture;
import matlab.unittest.constraints.EndsWithSubstring;
import matlab.unittest.constraints.ContainsSubstring;

suite = TestSuite.fromClass(?ExampleTest)
suite = 

  1x3 Test array with properties:

    Name
    Parameterization
    SharedTestFixtures

The test suite contains three test elements.

Create a filtered test suite of tests comprising tests with names that contain the case-insensitive substring 'pAtH'.

newSuite = selectIf(suite,HasName(ContainsSubstring('pAtH','IgnoringCase',true)))
newSuite = 

  Test with properties:

                  Name: 'ExampleTest/testPathAdd'
      Parameterization: []
    SharedTestFixtures: [1x1 matlab.unittest.fixtures.PathFixture]

Only the testPathAdd test is part of the suite.

Alternatively, create the same suite using a name-value pair.

newSuite = selectIf(suite,'Name','*Path*');

However, unlike the ContainsSubstring constraint, the name-value pair does not have an option to ignore case.

Create a filtered suite of tests comprising tests that use a shared path fixture and do not have names ending with 'One'.

newSuite = suite.selectIf(~HasName(EndsWithSubstring('One')) ...
    & HasSharedTestFixture(PathFixture(fullfile(matlabroot, 'help',...
    'techdoc', 'matlab_oop', 'examples'))));
{newSuite.Name}
ans = 

    'ExampleTest/testPathAdd'    'ExampleTest/testTwo'

The test suite contains two tests. All of the tests use the specified path fixture, but the test named 'testOne' is excluded from the suite.

Select Test Elements Using Parameterization

In your working folder, create testZeros.m. This class contains four test methods.

classdef testZeros < matlab.unittest.TestCase
    properties (TestParameter)
        type = {'single','double','uint16'};
        outSize = struct('s2d',[3 3], 's3d',[2 5 4]);
    end
    
    methods (Test)
        function testClass(testCase, type, outSize)
            testCase.verifyClass(zeros(outSize,type), type);
        end
        
        function testSize(testCase, outSize)
            testCase.verifySize(zeros(outSize), outSize);
        end
        
        function testDefaultClass(testCase)
            testCase.verifyClass(zeros, 'double');
        end
        function testDefaultSize(testCase)
            testCase.verifySize(zeros, [1 1]);
        end
        
        function testDefaultValue(testCase)
            testCase.verifyEqual(zeros,0);
        end
    end
end

The test class contains two parameterized test methods, testClass and testSize.

At the command prompt, create a test suite from the file.

s = matlab.unittest.TestSuite.fromFile('testZeros.m');
{s.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=single,outSize=s3d)'
    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'
    'testZeros/testClass(type=uint16,outSize=s2d)'
    'testZeros/testClass(type=uint16,outSize=s3d)'
    'testZeros/testSize(outSize=s2d)'
    'testZeros/testSize(outSize=s3d)'
    'testZeros/testDefaultClass'
    'testZeros/testDefaultSize'
    'testZeros/testDefaultValue'

The suite contains 11 test elements. Six from the parameterized testClass method, two from the parameterized testSize method, and one from each of the testDefaultClass, testDefaultSize, and testDefaultValue methods.

Select all of the test elements from parameterized test methods.

import matlab.unittest.selectors.HasParameter;

s1 = s.selectIf(HasParameter);
{s1.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=single,outSize=s3d)'
    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'
    'testZeros/testClass(type=uint16,outSize=s2d)'
    'testZeros/testClass(type=uint16,outSize=s3d)'
    'testZeros/testSize(outSize=s2d)'
    'testZeros/testSize(outSize=s3d)'

The suite contains the eight test elements from the two parameterized test methods.

Select all of the test elements from non-parameterized test methods.

s2 = s.selectIf(~HasParameter);
{s2.Name}'
ans = 

    'testZeros/testDefaultClass'
    'testZeros/testDefaultSize'
    'testZeros/testDefaultValue'

Select all test elements that are parameterized and have a property named 'type' with a parameter name 'double'.

s3 = s.selectIf('ParameterProperty','type', 'ParameterName','double');
{s3.Name}'
ans = 

    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'

The resulting suite contains two elements. The testClass method is the only method in testZeros that uses the 'type' property, and selecting only 'double' from the parameters results in two test elements — one for each value of 'outSize'.

Select all test elements that are parameterized and have a parameters defined by a property starting with 't'.

s4 = s.selectIf('ParameterProperty','t*');
{s4.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=single,outSize=s3d)'
    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s3d)'
    'testZeros/testClass(type=uint16,outSize=s2d)'
    'testZeros/testClass(type=uint16,outSize=s3d)'

The resulting suite contains the six parameterized test elements from the testClass method. The testSize method is parameterized, but the elements from the method are not included in the suite because the method does not use a property that starts with 't'.

Select all test elements that are parameterized and test the zeros function with a 2-D array. A parameter value representing a 2-D array will have a length of 1 (e.g. zeros(3)) or 2 (e.g. zeros(2,3)).

import matlab.unittest.constraints.HasLength;

s5 = s.selectIf(HasParameter('Property','outSize',...
    'Value', HasLength(1)|HasLength(2)));
{s5.Name}'
ans = 

    'testZeros/testClass(type=single,outSize=s2d)'
    'testZeros/testClass(type=double,outSize=s2d)'
    'testZeros/testClass(type=uint16,outSize=s2d)'
    'testZeros/testSize(outSize=s2d)'

Select only the test element that tests that the output is a double data type and the has the correct size for a 2-D array.

s6 = s.selectIf(HasParameter('Property','type','Name','double')...
    & HasParameter('Property','outSize','Name','s2d'))
s6 = 

  Test with properties:

                  Name: 'testZeros/testClass(type=double,outSize=s2d)'
      Parameterization: [1x2 matlab.unittest.parameters.TestParameter]
    SharedTestFixtures: []

See Also

|

Was this topic helpful?