Main Content

matlab.unittest.selectors.HasSuperclass class

Package: matlab.unittest.selectors

Selector for TestSuite elements determined by test class hierarchy

Construction

matlab.unittest.selectors.HasSuperclass(n) creates a selector that filters TestSuite array elements by retaining only those elements whose test class derives from n.

Input Arguments

expand all

Superclass name, specified as a character vector or string scalar.

Copy Semantics

Value. To learn how value classes affect copy operations, see Copying Objects.

Examples

collapse all

Create the following handle class in your current folder.

classdef MyClass < handle
    properties
        % properties of class
    end
    methods
        % methods of class
    end
end

Create the following test class in your current folder.

classdef Feature1_Test < matlab.unittest.TestCase
    methods (Test)
        function testA1(testCase)
            % test code
        end
        function testB1(testCase)
            % test code
        end
    end
end

Create the following test class in your current folder. This test class subclasses MyClass.

classdef Feature2_Test < matlab.unittest.TestCase & MyClass
    methods (Test)
        function testA2(testCase)
            % test code
        end
        function testB2(testCase)
            % test code
        end
    end
end

Create a test suite from all the files in your current folder. Depending on what files are in your folder, your test suite might differ.

import matlab.unittest.TestSuite;
suite = TestSuite.fromFolder(pwd)
suite = 

  1×4 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

Tests Include:
   0 Parameterizations, 0 Shared Test Fixture Classes, 0 Tags.

View the names of the test elements in the suite.

{suite.Name}'
ans =

  4×1 cell array

    {'Feature1_Test/testA1'}
    {'Feature1_Test/testB1'}
    {'Feature2_Test/testA2'}
    {'Feature2_Test/testB2'}

Create another test suite that contains only the suite elements that have MyClass in the class hierarchy. View the test element names. Since the test class Feature2_Test is the only test class to subclass MyClass, only test elements from that class are included in the suite.

newSuite = suite.selectIf(HasSuperclass('MyClass'));
{newSuite.Name}'
ans =

  2×1 cell array

    {'Feature2_Test/testA2'}
    {'Feature2_Test/testB2'}

Alternatively, use the testsuite function to create the filtered suite directly.

newSuite = testsuite(pwd,'Superclass','MyClass');
Introduced in R2018a