Main Content

matlab.unittest.selectors.HasSuperclass Class

Namespace: matlab.unittest.selectors

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.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

selector = matlab.unittest.selectors.HasSuperclass(class) 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 the HasSuperclass 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 the MyClass 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 the Feature1Test test class by subclassing the matlab.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 the Feature2Test test class. This test class subclasses MyClass 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 the TestSuite 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. Because Feature2Test is the only test class that subclasses MyClass, the filtered test suite includes only the tests defined in the Feature2Test 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 the Superclass 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