matlab.unittest.selectors.HasSharedTestFixture class

Package: matlab.unittest.selectors

Selector for TestSuite elements that use shared test fixture

Description

The HasSharedTestFixture selector filters TestSuite array elements based on shared test fixtures.

Construction

matlab.unittest.selectors.HasSharedTestFixture(f) constructs a selector for TestSuite elements based on their required shared test fixtures. For an element to be selected for the TestSuite array, it must use a fixture that is compatible with the specified fixture, f.

Input Arguments

expand all

Shared test fixture, specified as a matlab.unittest.fixtures.Fixture instance. The TestSuite array element must use a fixture compatible with f to be selected for the TestSuite.

Properties

ExpectedFixture

Shared test fixture, specified as a matlab.unittest.fixtures.Fixture instance in the input argument, f. The TestSuite array element must use a fixture compatible with the ExpectedFixture property to be selected for the TestSuite.

Copy Semantics

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

Examples

collapse all

Create a package folder, +mytestpackage, in your current folder. This package contains two test classes.

In the +mytestpackage folder, create a test class named AExampleTest. This class contains two tests that use a suppressed warnings fixture.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        'MATLAB:rmpath:DirNotFound')}) ...
        AExampleTest < matlab.unittest.TestCase
    methods (Test)
        function testOne(testCase)
            % test code
        end
        function testTwo(testCase)
            % test code
        end
    end
end

In the +mytestpackage folder, create a test class named BExampleTest. This class contains one test that uses a shared path fixture and a suppressed warnings fixture.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture( ...
        fullfile(matlabroot,'help','techdoc','matlab_oop','examples')),...
        matlab.unittest.fixtures.SuppressedWarningsFixture( ...
        'MATLAB:rmpath:DirNotFound')}) ...
        BExampleTest < matlab.unittest.TestCase
    methods(Test)
        function testPathAdd(testCase)
            % test code
        end
    end
end

At the command prompt, define the following fixtures.

pf = matlab.unittest.fixtures.PathFixture(...
    fullfile(matlabroot,'help','techdoc','matlab_oop','examples'));
swf = matlab.unittest.fixtures.SuppressedWarningsFixture(...
    'MATLAB:rmpath:DirNotFound');

Create a test suite from the package.

import matlab.unittest.TestSuite
import matlab.unittest.selectors.HasSharedTestFixture

suite = TestSuite.fromPackage('mytestpackage')
suite = 

  1×3 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

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

The test suite has three test elements.

Create a filtered suite that only contains tests that use the path fixture, pf.

s1 = suite.selectIf(HasSharedTestFixture(pf));

The resulting suite, s1, contains the test element from BExampleTest, since the test in that class uses the shared test fixture, pf.

Alternatively, pass the selector to the TestSuite.fromPackage method instead of generating a full test suite, and then using the TestSuite.selectIf method to filter the suite.

s1 = TestSuite.fromPackage('mytestpackage',HasSharedTestFixture(pf));

Create a filtered test suite that contains tests that use the suppressed warnings fixture, swf, but not the path fixture, pf.

s2 = suite.selectIf(~HasSharedTestFixture(pf) & HasSharedTestFixture(swf));

The test suite, s2, only contains the two test elements from AExampleTest. Tests in BExampleTest are excluded because, in addition to the suppressed warnings fixture, they use the path fixture.

Create a filtered suite that only contains tests that use the path fixture to a different location.

pf2 = matlab.unittest.fixtures.PathFixture(fullfile(matlabroot));
s3 =  TestSuite.fromPackage('mytestpackage', HasSharedTestFixture(pf2))
s3 = 

  1×0 Test array with properties:

    Name
    ProcedureName
    TestClass
    BaseFolder
    Parameterization
    SharedTestFixtures
    Tags

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

The test suite does not contain any test elements. The tests in BExampleTest use a shared path fixture, but the selected path fixture, pf2, adds a different folder to the path so its tests are not included in the suite.

Introduced in R2014a