Main Content

matlab.unittest.selectors.HasSharedTestFixture Class

Namespace: matlab.unittest.selectors

Select TestSuite array elements by shared test fixture

Description

The matlab.unittest.selectors.HasSharedTestFixture class provides a selector for filtering a test suite based on shared test fixtures.

For a class-based test, shared test fixtures are all the fixtures specified by the SharedTestFixtures class-level attribute of the corresponding TestCase class.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

selector = matlab.unittest.selectors.HasSharedTestFixture(expectedFixture) creates a selector that selects TestSuite array elements that use a shared test fixture compatible with expectedFixture and sets the ExpectedFixture property. Two fixtures are compatible if they are of the same class and if they make the same changes to the environment.

example

Properties

expand all

Shared test fixture, returned as a matlab.unittest.fixtures.Fixture object. Specify the value of this property during creation of the selector.

Attributes:

GetAccess
public
SetAccess
private

Examples

collapse all

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

Create a folder named myTests in your current folder. Then, in a file named Feature1Test.m in myTests, create the Feature1Test class. The class contains tests that use a fixture for suppressing a specified warning.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        "MATLAB:rmpath:DirNotFound")}) ...
        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 the myTests folder, create the Feature2Test class. The class contains tests that use a fixture for creating a temporary folder and another fixture for suppressing a specified warning.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.TemporaryFolderFixture( ...
        "WithSuffix","_TestData"), ...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        "MATLAB:rmpath:DirNotFound")}) ...
        Feature2Test < 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

Import the classes used in this example.

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasSharedTestFixture
import matlab.unittest.fixtures.SuppressedWarningsFixture
import matlab.unittest.fixtures.TemporaryFolderFixture

Create fixtures compatible with the shared test fixtures used in this example.

warnf = SuppressedWarningsFixture("MATLAB:rmpath:DirNotFound");
tempf = TemporaryFolderFixture("WithSuffix","_TestData");

Create a test suite from the myTests folder. Then, display the names of the TestSuite array elements. The test suite contains four tests.

suite = testsuite("myTests");
disp({suite.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

Select all the tests that use a fixture compatible with the fixture tempf. The filtered test suite contains the tests from Feature2Test because this class has a shared test fixture that is compatible with tempf.

suite1 = suite.selectIf(HasSharedTestFixture(tempf));
disp({suite1.Name}')
    {'Feature2Test/defaultBehavior'}
    {'Feature2Test/otherBehavior'  }

Select all the tests that use a fixture compatible with warnf, but not tempf. This condition is satisfied only by the tests in the Feature1Test class.

suite2 = suite.selectIf( ...
    ~HasSharedTestFixture(tempf) & HasSharedTestFixture(warnf));
disp({suite2.Name}')
    {'Feature1Test/defaultBehavior'}
    {'Feature1Test/otherBehavior'  }

Create a filtered test suite directly from myTests by including only tests that use a fixture for creating a basic temporary folder. The resulting test suite is empty because the selector uses a fixture that is not compatible with any shared test fixtures. Even though the tests in Feature2Test use a fixture for creating a temporary folder, their fixture includes a suffix for the temporary folder name.

suite3 = TestSuite.fromFolder("myTests", ...
    HasSharedTestFixture(TemporaryFolderFixture))
suite3 = 

  1×0 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

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

Version History

Introduced in R2014a