Main Content

sortByFixtures

Class: matlab.unittest.TestSuite
Namespace: matlab.unittest

Reorder test suite based on shared fixtures

Syntax

sortedSuite = sortByFixtures(suite)
[sortedSuite,I] = sortByFixtures(suite)

Description

sortedSuite = sortByFixtures(suite) reorders the test suite to reduce shared fixture setup and teardown operations. Do not rely on the order of elements in sortedSuite as it might change in a future release.

[sortedSuite,I] = sortByFixtures(suite) also returns a sort index I that describes the arrangement of the elements of suite into sortedSuite.

Input Arguments

expand all

Set of tests, specified as a matlab.unittest.Test array.

Output Arguments

expand all

Ordered set of tests, returned as a matlab.unittest.Test array. sortedSuite is a permutation of the test elements of suite.

Sort index, returned as a vector, matrix, or multidimensional array. I is the same size as suite and describes the arrangement of the elements of suite into orderedSuite. Specifically, sortedSuite = suite(I).

Examples

expand all

Create three test classes in your current working folder. Test classes MyTestClassA and MyTestClassC use the same shared path fixture.

classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture('offPathFolder')}) ...
        MyTestClassA < matlab.unittest.TestCase
    methods (Test)
        function test_A(testCase)
            % test content
        end
    end
end
classdef MyTestClassB < matlab.unittest.TestCase
    methods (Test)
        function test_B(testCase)
            % test content
        end
    end
end
classdef (SharedTestFixtures={ ...
        matlab.unittest.fixtures.PathFixture('offPathFolder')}) ...
        MyTestClassC < matlab.unittest.TestCase
    methods (Test)
        function test_C(testCase)
            % test content
        end
    end
end

Create a test suite from each class.

import matlab.unittest.TestSuite;
suiteA = TestSuite.fromClass(?MyTestClassA);
suiteB = TestSuite.fromClass(?MyTestClassB);
suiteC = TestSuite.fromClass(?MyTestClassC);

Concatenate the suites and view the order of test elements.

suite = [suiteA suiteB suiteC];
{suite.Name}'
ans =

  3×1 cell array

    {'MyTestClassA/test_A'}
    {'MyTestClassB/test_B'}
    {'MyTestClassC/test_C'}

Sort the suite by shared fixtures and view the order of test elements.

sortedSuite = sortByFixtures(suite);
{sortedSuite.Name}'
ans =

  3×1 cell array

    {'MyTestClassA/test_A'}
    {'MyTestClassC/test_C'}
    {'MyTestClassB/test_B'}

Since the tests in MyTestClassA and MyTestClassC have the same shared test fixture, the test elements are reordered so that they are adjacent in the suite.

Tips

If you create a test suite using a single call to the testsuite function instead of several calls to a method of matlab.unittest.TestSuite, the suite is automatically sorted based on shared fixtures. However, if you add, remove, or reorder elements after initial suite creation, call the sortByFixtures method to sort the suite.

Version History

Introduced in R2018b