matlab.unittest.selectors.HasSuperclass - Select TestSuite array elements by test superclass - MATLAB (original) (raw)

Main Content

Namespace: matlab.unittest.selectors
Superclasses: matlab.unittest.selectors.Selector

Select TestSuite array elements by test superclass

Description

The matlab.unittest.selectors.HasSuperclass class provides a selector for filtering a test suite based on test superclasses.

For a class-based test, test superclasses are all the superclasses of the class that defines the test. For more information about class hierarchies, see Hierarchies of Classes — Concepts.

Creation

Description

selector = matlab.unittest.selectors.HasSuperclass([class](#mw%5F061a17bd-a647-4806-8f4b-69ec208aafd5)) creates a selector that selects TestSuite array elements with the specified test superclass.

example

Input Arguments

expand all

Test superclass, specified as a string scalar or character vector.

Examples

collapse all

Create filtered test suites by selecting tests using theHasSuperclass class. To simplify the test code, the test classes in this example use unconditional test failures as placeholders for unimplemented tests.

In a file named MyClass.m in your current folder, create theMyClass class.

classdef MyClass < handle properties % Properties of MyClass end

methods
    % Methods of MyClass
end

end

In a file named Feature1Test.m in your current folder, create theFeature1Test test class by subclassing thematlab.unittest.TestCase class.

classdef Feature1Test < matlab.unittest.TestCase methods (Test) function defaultBehavior(testCase) testCase.verifyFail("Add code to test default behavior.") end function otherBehavior(testCase) testCase.verifyFail("Add code to test nondefault behavior.") end end end

In a file named Feature2Test.m in your current folder, create theFeature2Test test class. This test class subclassesMyClass in addition to the matlab.unittest.TestCase class.

classdef Feature2Test < matlab.unittest.TestCase & MyClass methods (Test) function defaultBehavior(testCase) testCase.verifyFail("Add code to test default behavior.") end function otherBehavior(testCase) testCase.verifyFail("Add code to test nondefault behavior.") end end end

Import the classes used in this example.

import matlab.unittest.TestSuite import matlab.unittest.selectors.HasSuperclass

Create a test suite from the test classes. Then, display the names of theTestSuite array elements. The test suite contains four tests.

suite = testsuite(["Feature1Test" "Feature2Test"]); disp({suite.Name}')

{'Feature1Test/defaultBehavior'}
{'Feature1Test/otherBehavior'  }
{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior'  }

Select all the tests that have MyClass as a test superclass. BecauseFeature2Test is the only test class that subclassesMyClass, the filtered test suite includes only the tests defined in theFeature2Test test class.

suite1 = suite.selectIf(HasSuperclass("MyClass")); disp({suite1.Name}')

{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior'  }

Create a filtered test suite directly from the tests in your current folder by including only tests that have MyClass as a test superclass.

suite2 = TestSuite.fromFolder(pwd,HasSuperclass("MyClass")); disp({suite2.Name}')

{'Feature2Test/defaultBehavior'}
{'Feature2Test/otherBehavior'  }

Alternative Functionality

In addition to the HasSuperclass class, you can use theSuperclass name-value argument to create a filtered test suite based on test superclasses. For example:

filteredSuite = matlab.unittest.TestSuite.fromFolder(pwd, ... "Superclass","MyClass");

You can also select and run tests using the Superclass name-value argument of the runtests or runperf function. For example:

results = runtests(pwd,"Superclass","MyClass");

Version History

Introduced in R2018a