matlab.unittest.TestSuite.selectIf - Select test suite elements that satisfy conditions - MATLAB (original) (raw)

Class: matlab.unittest.TestSuite
Namespace: matlab.unittest

Select test suite elements that satisfy conditions

Syntax

Description

filteredSuite = selectIf([suite](#mw%5Fa85899c5-2b42-49a4-b8a3-a8da0d363fd6),[selector](#bt44o27-1%5Fsep%5Fmw%5F5a5f33c9-88ba-4611-8b39-30ba9c575eee)) selects the elements of the specified test suite that satisfy the conditions set by the specified selector. The method returns the selected elements as a filtered TestSuite array.

example

filteredSuite = selectIf([suite](#mw%5Fa85899c5-2b42-49a4-b8a3-a8da0d363fd6),[Name,Value](#namevaluepairarguments)) filters the test suite using options specified by one or more name-value arguments. For example, filteredSuite = selectIf(suite,"Name","featureA_*") selects the elements of suite whose name starts with"featureA_".

example

Input Arguments

Name-Value Arguments

expand all

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Example: filteredSuite = selectIf(suite,Name="featureA_*")

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: filteredSuite = selectIf(suite,"Name","featureA_*")

Name of the base folder that contains the test file, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Test element must be contained in one of the base folders specified byBaseFolder. If none of theTest elements match a base folder, an empty test suite is returned. Use the wildcard character (*) to match any number of characters. Use the question mark character (?) to match a single character.

For test files defined in namespaces, the base folder is the parent of the top-level namespace folder.

Names of the files and folders that contain source code, specified as a string vector, character vector, or cell vector of character vectors. This argument filters the test suite by including only the tests that depend on the specified source code. If none of the tests depend on the source code, an empty test suite is returned.

The specified value must represent at least one existing file. If you specify a folder, the framework extracts the paths to the files within the folder.

You must have a MATLAB® Test™ license to use DependsOn. For more information about selecting tests by source code dependency, see matlabtest.selectors.DependsOn (MATLAB Test).

Example: DependsOn=["myFile.m" "myFolder"]

Example: DependsOn=["folderA" "C:\work\folderB"]

Name of the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Name property of theTest element must match one of the names specified byName. If none of the Test elements have a matching name, an empty test suite is returned. Use the wildcard character (*) to match any number of characters. Use the question mark character (?) to match a single character.

For a given test file, the name of a test uniquely identifies the smallest runnable portion of the test content. The test name includes the namespace name, filename (excluding the extension), procedure name, and information about parameterization.

Name of a test class property that defines a parameter used by the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, theParameterization property of the Test element must contain at least one of the property names specified byParameterProperty. If none of the Test elements have a matching property name, an empty test suite is returned. Use the wildcard character (*) to match any number of characters. Use the question mark character (?) to match a single character.

Name of a parameter used by the test, specified as a string array, character vector, or cell array of character vectors. MATLAB generates parameter names based on the test class property that defines the parameters. For example:

The ParameterName argument filters the test suite. For the testing framework to include a test in the filtered suite, theParameterization property of theTest element must contain at least one of the parameter names specified by ParameterName. If none of the Test elements have a matching parameter name, an empty test suite is returned. Use the wildcard character (*) to match any number of characters. Use the question mark character (?) to match a single character.

Name of the class that the test class derives from, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, theTestClass property of the Test element must point to a test class that derives from one of the classes specified bySuperclass. If none of the Test elements match a class, an empty test suite is returned.

Name of a tag used by the test, specified as a string array, character vector, or cell array of character vectors. This argument filters the test suite. For the testing framework to include a test in the filtered suite, the Tags property of the Test element must contain at least one of the tag names specified by Tag. If none of the Test elements have a matching tag name, an empty test suite is returned. Use the wildcard character (*) to match any number of characters. Use the question mark character (?) to match a single character.

Examples

expand all

Filter a test suite by using the selectIf method.

In a file named FeatureTest.m in your current folder, create theFeatureTest class, which contains tests that use a shared test fixture. To simplify the test code, the test class uses unconditional test failures as placeholders for unimplemented tests.

classdef (SharedTestFixtures={ ... matlab.unittest.fixtures.SuppressedWarningsFixture( ... "MATLAB:rmpath:DirNotFound")}) ... FeatureTest < matlab.unittest.TestCase methods (Test) function testA(testCase) testCase.verifyFail("Implement the test.") end function testB(testCase) testCase.verifyFail("Implement the test.") end function testC(testCase) testCase.verifyFail("Implement the test.") end end end

Import the classes used in this example.

import matlab.unittest.selectors.HasName import matlab.unittest.constraints.ContainsSubstring import matlab.unittest.constraints.EndsWithSubstring import matlab.unittest.selectors.HasSharedTestFixture import matlab.unittest.fixtures.SuppressedWarningsFixture

Create a test suite from the FeatureTest class and display the test names. The suite contains three Test elements.

suite = testsuite("FeatureTest"); disp({suite.Name}')

{'FeatureTest/testA'}
{'FeatureTest/testB'}
{'FeatureTest/testC'}

Create a filtered test suite by selecting tests whose name contains the case-insensitive text "TA".

suite1 = selectIf(suite,HasName(ContainsSubstring("TA","IgnoringCase",true))); disp({suite1.Name}')

Create a filtered test suite by selecting tests whose name contains"A" or "B".

suite2 = selectIf(suite,"Name",["A" "B"]); disp({suite2.Name}')

{'FeatureTest/testA'}
{'FeatureTest/testB'}

Create a filtered suite of tests that use a specified shared test fixture and do not have names ending with "C". All of the tests use the specified fixture, but "testC" is excluded from the suite because its name does not satisfy the naming constraint.

suite3 = suite.selectIf(~HasName(EndsWithSubstring("C")) & ... HasSharedTestFixture(SuppressedWarningsFixture( ... "MATLAB:rmpath:DirNotFound"))); disp({suite3.Name}')

{'FeatureTest/testA'}
{'FeatureTest/testB'}

Create filtered test suites by selecting tests based on parameterization.

In a file named ZerosTest.m in your current folder, create theZerosTest class, which tests the zeros function. The class has two parameterized Test methods:testClass and testSize.

classdef ZerosTest < matlab.unittest.TestCase properties (TestParameter) type = {'single','double','uint16'}; size = struct("s2d",[3 3],"s3d",[2 5 4]); end

methods (Test)
    function testClass(testCase,size,type)
        testCase.verifyClass(zeros(size,type),type)
    end
    
    function testSize(testCase,size)
        testCase.verifySize(zeros(size),size)
    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

Import the HasParameter class.

import matlab.unittest.selectors.HasParameter

Create a test suite from the ZerosTest class and display the test names. The suite contains 11 Test elements.

suite = testsuite("ZerosTest"); disp({suite.Name}')

{'ZerosTest/testClass(size=s2d,type=single)'}
{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s2d,type=uint16)'}
{'ZerosTest/testClass(size=s3d,type=single)'}
{'ZerosTest/testClass(size=s3d,type=double)'}
{'ZerosTest/testClass(size=s3d,type=uint16)'}
{'ZerosTest/testSize(size=s2d)'             }
{'ZerosTest/testSize(size=s3d)'             }
{'ZerosTest/testDefaultClass'               }
{'ZerosTest/testDefaultSize'                }
{'ZerosTest/testDefaultValue'               }

Create a filtered test suite by selecting all the parameterized tests.

suite1 = suite.selectIf(HasParameter); disp({suite1.Name}')

{'ZerosTest/testClass(size=s2d,type=single)'}
{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s2d,type=uint16)'}
{'ZerosTest/testClass(size=s3d,type=single)'}
{'ZerosTest/testClass(size=s3d,type=double)'}
{'ZerosTest/testClass(size=s3d,type=uint16)'}
{'ZerosTest/testSize(size=s2d)'             }
{'ZerosTest/testSize(size=s3d)'             }

Select all the tests that are not parameterized.

suite2 = suite.selectIf(~HasParameter); disp({suite2.Name}')

{'ZerosTest/testDefaultClass'}
{'ZerosTest/testDefaultSize' }
{'ZerosTest/testDefaultValue'}

Select all the parameterized tests with the parameterization property"type" and the parameter name "double".

suite3 = suite.selectIf(HasParameter("Property","type","Name","double")); disp({suite3.Name}')

{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s3d,type=double)'}

Select all the parameterized tests with a parameterization property whose name starts with "t".

suite4 = suite.selectIf("ParameterProperty","t*"); disp({suite4.Name}')

{'ZerosTest/testClass(size=s2d,type=single)'}
{'ZerosTest/testClass(size=s2d,type=double)'}
{'ZerosTest/testClass(size=s2d,type=uint16)'}
{'ZerosTest/testClass(size=s3d,type=single)'}
{'ZerosTest/testClass(size=s3d,type=double)'}
{'ZerosTest/testClass(size=s3d,type=uint16)'}

Create filtered test suites by selecting tests based on test tags.

In a file named ExampleTest.m in your current folder, create theExampleTest class, which uses the TestTags method-level attribute to tag individual tests. To simplify the test code, theTest methods in this example use unconditional test failures as placeholders for unimplemented tests.

classdef ExampleTest < matlab.unittest.TestCase methods (Test) function testA(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags="Unit") function testB(testCase) testCase.verifyFail("Implement the test.") end function testC(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags=["Unit" "FeatureA"]) function testD(testCase) testCase.verifyFail("Implement the test.") end end methods (Test,TestTags=["System" "FeatureB"]) function testE(testCase) testCase.verifyFail("Implement the test.") end end end

Import the HasTag class.

import matlab.unittest.selectors.HasTag

Create a test suite from the ExampleTest class and display the test names. The suite contains five Test elements.

suite = testsuite("ExampleTest"); disp({suite.Name}')

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

Select all the tests that have the tag "Unit".

suite1 = suite.selectIf("Tag","Unit"); disp({suite1.Name}')

{'ExampleTest/testD'}
{'ExampleTest/testB'}
{'ExampleTest/testC'}

Select all the tests that do not have the tag "FeatureA".

suite2 = suite.selectIf(~HasTag("FeatureA")); disp({suite2.Name}')

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

Select all the tests that have no tags.

suite3 = suite.selectIf(~HasTag); disp({suite3.Name}')

Version History

Introduced in R2014a

expand all

When you select function-based or class-based tests using theDependsOn name-value argument or thematlabtest.selectors.DependsOn class (requires MATLAB Test), the method more accurately selects tests that depend on the specified source code. If the method can determine which individual tests in the test file depend on the source code, then it selects only the dependent tests and excludes the rest. Otherwise, the method includes all the tests in the test file.

In previous releases, the method includes all the tests in a test file if the file depends on the specified source code, without attempting to exclude tests that are not dependent on the source code.

If you have a MATLAB Test license, you can specify any type of source file using theDependsOn name-value argument or thematlabtest.selectors.DependsOn class. In previous releases, you can specify files only with a .m, .p,.mlx, .mlapp, .mat, or.slx extension.

If you have a MATLAB Test license, you can filter a test suite by test file dependency on specified source code. Use the DependsOn name-value argument or thematlabtest.selectors.DependsOn class to specify the source files and folders.

When you assign a nonempty cell array to a parameterization property, the testing framework generates parameter names from the elements of the cell array by taking into account their values, types, and dimensions. In previous releases, if the property value is a cell array of character vectors, the framework generates parameter names from the values in the cell array. Otherwise, the framework specifies parameter names as value1, value2, …, valueN.

If your code uses parameter names to create or filter test suites, replace the old parameter names with the descriptive parameter names. For example, update suite = testsuite(pwd,"ParameterName","value1") by replacing value1 with a descriptive parameter name.